File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Converting char[] to byte[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting char[] to byte[]" Watch "Converting char[] to byte[]" New topic
Author

Converting char[] to byte[]

Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
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>


My Thoughts : http://passion4java.blogspot.com
Try not to become a man of success but rather to become a man of value.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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



Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
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
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 07, 2004
Posts: 781

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

Joined: Oct 17, 2007
Posts: 198
James you are right, i am trying to do the same....thanks a lot your comments helps me....;)
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Dec 31, 2004
Posts: 961
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Converting char[] to byte[]
 
Similar Threads
empty vector cells
How to read bytes to string regardless of encoding?
Q how does the above code give error and the code below doesn't???
Convert from char to byte?
How to write back an updated record to db file?