How to byte[ ] --> char[ ] and char[ ] --> byte[ ]?

Discussion in 'Java' started by Richard, Jan 30, 2006.

  1. Richard

    Richard Guest

    Hello,

    I get a char[] from a JPasswordField and I would like to use
    MessageDigest.digest(byte[]) to get a md5 digest of the password. What's
    the best way to transform a char[] into a byte[] in Java (and to
    transform a byte[] into a char[])?

    I don't want to use a String to do that.

    Thanks in advance for your answers.

    Richard
     
    Richard, Jan 30, 2006
    #1
    1. Advertising

  2. Richard

    Jeroen V. Guest

    This is a way, don't know whether it's the best (guess you mean most
    performant) way...

    char[] chars = "azertyuiop".toCharArray();
    byte[] bytes = new byte[10];
    int i = 0;
    for(char c : chars) bytes[i++] = (byte)c;
    i = 0;
    for(byte b : bytes) chars[i++] = (char)b;
    System.out.println(chars);
     
    Jeroen V., Jan 30, 2006
    #2
    1. Advertising

  3. A thing to keep in mind is are you sure what encoding your characters
    are going to be? char is 16 bit. byte is 8 bit. (in java)

    Cheers

    Opalinski

    http://www.geocities.com/opalpaweb/
     
    opalinski from opalpaweb, Jan 30, 2006
    #3
  4. Richard

    Richard Guest

    OK, I know that, thanks. My problem is to get a md5 digest of a password
    registered in a char[]. This problem seems simple but can't find a
    simple solution to do it. Does it exist one? Else, do you know a not so
    simple one?

    Another question: does a md5 digest can be registered in a String? Or
    does it contains bytes that can't be registered in a String?

    Cheers,

    Richard

    opalinski from opalpaweb a écrit :
    > A thing to keep in mind is are you sure what encoding your characters
    > are going to be? char is 16 bit. byte is 8 bit. (in java)
    >
    > Cheers
    >
    > Opalinski
    >
    > http://www.geocities.com/opalpaweb/
    >
     
    Richard, Jan 30, 2006
    #4
  5. Richard

    Roedy Green Guest

    On Mon, 30 Jan 2006 17:37:37 +0100, Richard <> wrote,
    quoted or indirectly quoted someone who said :

    >I get a char[] from a JPasswordField and I would like to use
    >MessageDigest.digest(byte[]) to get a md5 digest of the password. What's
    >the best way to transform a char[] into a byte[] in Java (and to
    >transform a byte[] into a char[])?


    see http://mindprod.com/jgloss/conversion.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 30, 2006
    #5
  6. Richard

    Roedy Green Guest

    On Mon, 30 Jan 2006 21:31:28 +0100, Richard <> wrote,
    quoted or indirectly quoted someone who said :

    >
    >Another question: does a md5 digest can be registered in a String? Or
    >does it contains bytes that can't be registered in a String?


    md5 and sha1 give you a byte[] of what appear at first to be random
    bytes. If you to transport that in printable format, you would need
    some form of armouring.

    See http://mindprod.com/jgloss/armouring.html
    http://mindprod.com/jgloss/md5.html
    http://mindprod.com/jgloss/sha1.html

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 30, 2006
    #6
  7. Richard

    Venky Guest


    > I don't want to use a String to do that.


    Any special reason why you don't want to use String to convert char[]
    to bytes[].
    Something like this.
    MessageDigest.digest((new String(char[])).getBytes());
     
    Venky, Jan 31, 2006
    #7
  8. Richard

    Adam Maass Guest

    "Richard" <> wrote:
    >
    > I get a char[] from a JPasswordField and I would like to use
    > MessageDigest.digest(byte[]) to get a md5 digest of the password. What's
    > the best way to transform a char[] into a byte[] in Java (and to transform
    > a byte[] into a char[])?


    You need to decide which /encoding/ you are going to use to do the
    transformation from char[] to byte[] (and back again). char is 16-bits; byte
    is 8-bits; there are any number of ways to map the 16-bit values onto 8-bit
    values.

    One such encoding is ASCII (though based on the .fr domain in your email,
    probably not a good choice). Then the code to get a byte[] from a char[] is
    very simple:

    byte[] bytes = new String(chars).getBytes("ASCII");

    There is also the not-quite analogous getChars method...

    String str = new String(bytes, "ASCII");
    char[] chars = new char[str.length()];
    str.getChars(0, chars.length, chars, chars.length);

    >
    > I don't want to use a String to do that.
    >


    The String class has the very handy getBytes() and getChars() methods, so
    you probably want to take advantage of that. Or you could look at the source
    for those methods and replicate it...



    -- Adam Maass
     
    Adam Maass, Jan 31, 2006
    #8
  9. > Any special reason why you don't want to use String to convert char[]
    > to bytes[].
    > Something like this.
    > MessageDigest.digest((new String(char[])).getBytes());


    probably same reason why JPasswordField has deprecated method String
    getText() - it is unsecure

    BTW the simplest method to convert chars to bytes and vise versa
    is to use CharToByteConverter and ByteToCharConverter.

    --
    Andrey Kuznetsov
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
     
    Andrey Kuznetsov, Jan 31, 2006
    #9
  10. > The String class has the very handy getBytes() and getChars() methods, so
    > you probably want to take advantage of that. Or you could look at the
    > source for those methods and replicate it...


    till java 1.4 they used CharToByteCoverter and ByteToCharConverter.

    --
    Andrey Kuznetsov
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
     
    Andrey Kuznetsov, Jan 31, 2006
    #10
  11. Richard

    Roedy Green Guest

    On Tue, 31 Jan 2006 10:46:59 +0100, "Andrey Kuznetsov"
    <> wrote, quoted or indirectly quoted someone
    who said :

    >BTW the simplest method to convert chars to bytes and vise versa
    >is to use CharToByteConverter and ByteToCharConverter.


    If you look at the String methods you might be horrified to see how
    much copying and creating of intermediate objects goes on during
    byte[] <=> String conversion.. These new methods can avoid much of
    that overhead.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 31, 2006
    #11
  12. Richard

    Adam Maass Guest

    "Roedy Green" wrote:
    > On Tue, 31 Jan 2006 10:46:59 +0100, "Andrey Kuznetsov"
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >
    >>BTW the simplest method to convert chars to bytes and vise versa
    >>is to use CharToByteConverter and ByteToCharConverter.

    >
    > If you look at the String methods you might be horrified to see how
    > much copying and creating of intermediate objects goes on during
    > byte[] <=> String conversion.. These new methods can avoid much of
    > that overhead.
    >


    byte[] <=> char[] conversion is hard to generalize, and yet String presents
    a pretty simple, general interface to the process. And String has that darn
    immutible property to maintain, so I would expect that the conversion
    routines in String to be less than totally efficient.

    -- Adam Maass
     
    Adam Maass, Feb 1, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    581
  2. wwj
    Replies:
    24
    Views:
    2,548
    Mike Wahler
    Nov 7, 2003
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,098
    Ian Collins
    May 9, 2006
  4. Replies:
    3
    Views:
    745
  5. Ioannis Vranos
    Replies:
    11
    Views:
    775
    Ioannis Vranos
    Mar 28, 2008
Loading...

Share This Page