Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting char[] to byte[]

 
Mohammed Yousuff
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i was trying to convert from char[] to a byte[] (with out using String class). i was unable to find any API which does this... then i written a sample code which converts from char[] to byte[]... it works...


in my code, i am looping the char[] and converting every element to byte... some where in the net i read that their are some chances to loose data while you convert like this?. is that true?...

let me know is there any better approach we have to do this conversion? thank you>
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Yousuff wrote:Hi i was trying to convert from char[] to a byte[] (with out using String class). i was unable to find any API which does this... then i written a sample code which converts from char[] to byte[]... it works...


in my code, i am looping the char[] and converting every element to byte... some where in the net i read that their are some chances to loose data while you convert like this?. is that true?...

let me know is there any better approach we have to do this conversion? thank you>


"is that true?" - Since char is wider than byte then sure you can loose data! Not all characters are ASCII or just 8 bits wide.

What is wrong with the direct approach of


 
Mohammed Yousuff
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot James, the reason is i want to pass my char[] to a method which expects byte[].... I understand that the same can be done using String class API, but we don't want to use that...Can you please suggest some other options if available.. thanks for your help.
 
Henry Wong
author
Marshal
Pie
Posts: 20897
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Yousuff wrote:Thanks a lot James, the reason is i want to pass my char[] to a method which expects byte[].... I understand that the same can be done using String class API, but we don't want to use that...Can you please suggest some other options if available.. thanks for your help.


Please read James post again.... in it, he mentioned that it is a matter of data. Sixteen unsigned bits can't fit into eight signed bits, no matter what technique you use. So, there are *no* other options available. If your data is pure text (ie. ASCII only) data, you will not lose anything during the conversion to bytes. If it contains any extended characters supported by unicode, you will lose them -- even with the String method call.

Henry
 
Mohammed Yousuff
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it Henry.Let me explain what i am trying to do...The current code accepts parameter as string and converts the string to byte[] using string.toByteArray()... What we are trying to is similar rather than accepting string we want to pass char[] and that should be converted to byte[]... currently all our inputs will not contain any internalization chars it only English and numbers.

i think in this scenario, the code which i have written will work without any issues?... thanks you
 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You keep asking that, and we keep answering it. If you meant to use ISO-8859-1 as your encoding, then you should be okay. If you meant to use some other encoding, or if you have no idea what an encoding is, then you probably won't be okay.

I'm also bemused why you would write a loop which might or might not work when you could simply do this:

Just saying "we don't want to use the String class" leads me to believe that somebody is having some strange idea leading to strange code.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Yousuff wrote:I got it Henry.Let me explain what i am trying to do...The current code accepts parameter as string and converts the string to byte[] using string.toByteArray()... What we are trying to is similar rather than accepting string we want to pass char[] and that should be converted to byte[]... currently all our inputs will not contain any internalization chars it only English and numbers.

i think in this scenario, the code which i have written will work without any issues?... thanks you


String does not have a method toByteArray() . I assume you meant to say that it uses one of the String.getBytes() methods.

It sounds to me like you are trying to be secure and not ever converting a password to a String. Plan for the future. Convert the char[] to a CharBuffer using the static CharBuffer.wrap() method, Convert to a ByteBuffer using Charset.encode() and extract the byte[] form the ByteBuffer.
 
Mohammed Yousuff
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James you are right, i am trying to do the same....thanks a lot your comments helps me....;)
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Yousuff wrote:James you are right, i am trying to do the same....thanks a lot your comments helps me....;)


I assume that the next 'feature' you will be trying to implement is to form a salted message digest. If so then you don't ever need the password as an explicit byte array since one of the MesageDigest update() methods accepts a ByteBuffer.

The whole conversion from char[] to salted message digest takes just 6 lines of code and that includes code to create a SecureRandom and the declaration and filling of the salt array.
 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Yousuff wrote:James you are right, i am trying to do the same....thanks a lot your comments helps me....;)

Aha... it always helps to know the context of a question.
... currently all our inputs will not contain any internalization chars it only English and numbers.

And these inputs are passwords? Then why would you prevent people from using characters which make the passwords harder to guess? Why can't I use the ~ character in my password, or even the ñ character?

Let me suggest that you should let people use whatever characters they like for their passwords. (If you want to inflict other rules on them, like they have to have a digit or a capital letter or whatever, that's fine too.) Then use UTF-8 as your encoding when you convert the password chars to bytes and you won't lose any information.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you intend not to use the String class (not sure why), you could use java.io to achieve the same result.



If you want to take encoding into account, then you just need to change the code a bit:



Assuming you want to use UTF-8.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic