Converting String to byte array

Discussion in 'Java' started by Raga, Oct 5, 2006.

  1. Raga

    Raga Guest

    Hi,

    There is a code for converting String to byte array, as follows:

    "
    public byte[] toByteArray(String s)
    {
    char[] c = s.toCharArray();
    int len = c.length;
    byte[] b = new byte[len * 2];

    for ( int i = 0 ; i < len ; i++ )
    {
    b[i * 2] = (byte)(c);
    b[(i * 2) + 1] = (byte)(c >> 8);
    }

    return b;
    }

    "

    But this isn't doing the conversion properly. For example, for the €
    (euro) symbol, it converts to some other unreadable symbol. Also, same
    is the case for square brackets. Any idea why this' so? What's wrong
    with the above code?

    Thanks.
    Raga, Oct 5, 2006
    #1
    1. Advertising

  2. Raga

    Raga Guest

    Raga wrote:
    > Hi,
    >
    > There is a code for converting String to byte array, as follows:
    >
    > "
    > public byte[] toByteArray(String s)
    > {
    > char[] c = s.toCharArray();
    > int len = c.length;
    > byte[] b = new byte[len * 2];
    >
    > for ( int i = 0 ; i < len ; i++ )
    > {
    > b[i * 2] = (byte)(c);
    > b[(i * 2) + 1] = (byte)(c >> 8);
    > }
    >
    > return b;
    > }
    >
    > "
    >
    > But this isn't doing the conversion properly. For example, for the €
    > (euro) symbol, it converts to some other unreadable symbol. Also, same
    > is the case for square brackets. Any idea why this' so? What's wrong
    > with the above code?
    >

    the encoding format is UTF-8.

    > Thanks.
    Raga, Oct 5, 2006
    #2
    1. Advertising

  3. Raga

    Oliver Wong Guest

    "Raga" <> wrote in message
    news:...
    >
    > Hi,
    >
    > There is a code for converting String to byte array, as follows:
    >
    > "
    > public byte[] toByteArray(String s)
    > {
    > char[] c = s.toCharArray();
    > int len = c.length;
    > byte[] b = new byte[len * 2];
    >
    > for ( int i = 0 ; i < len ; i++ )
    > {
    > b[i * 2] = (byte)(c);
    > b[(i * 2) + 1] = (byte)(c >> 8);
    > }
    >
    > return b;
    > }
    >
    > "
    >
    > But this isn't doing the conversion properly. For example, for the ?
    > (euro) symbol, it converts to some other unreadable symbol. Also, same
    > is the case for square brackets. Any idea why this' so? What's wrong
    > with the above code?
    >
    > the encoding format is UTF-8.


    Your code seems to be contradicting this claim. It seems to be encoding
    in Sun's special custom encoding (which is similar to, but distinct from,
    UTF-16).

    - Oliver
    Oliver Wong, Oct 5, 2006
    #3
  4. Raga wrote:

    > Raga wrote:
    >
    >>Hi,
    >>
    >>There is a code for converting String to byte array, as follows:
    >>
    >>"
    >> public byte[] toByteArray(String s)
    >> {
    >> char[] c = s.toCharArray();
    >> int len = c.length;
    >> byte[] b = new byte[len * 2];
    >>
    >> for ( int i = 0 ; i < len ; i++ )
    >> {
    >> b[i * 2] = (byte)(c);
    >> b[(i * 2) + 1] = (byte)(c >> 8);
    >> }
    >>
    >> return b;
    >> }
    >>
    >>"

    I think you have just re-invented the wheel. Your code above converts a
    String to a UTF-16 coded byte array. Your code above is equivalent to:
    public byte[] toByteArray(String s)
    {
    return s.getBytes("UTF-16");
    }
    Hence the question arises: Did you really want to decode the String to a
    UTF-16 coded byte array?
    >>
    >>But this isn't doing the conversion properly. For example, for the €
    >>(euro) symbol, it converts to some other unreadable symbol. Also, same
    >>is the case for square brackets. Any idea why this' so? What's wrong
    >>with the above code?
    >>

    >
    > the encoding format is UTF-8.

    Which encoding format is UTF-8? The one you get, or the one you want?
    May be you simply want:
    public byte[] toByteArray(String s)
    {
    return s.getBytes("UTF-8");
    }

    --
    Thomas
    Thomas Fritsch, Oct 5, 2006
    #4
    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. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,213
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    632
    Kirby
    Oct 8, 2004
  3. cryptogirl
    Replies:
    5
    Views:
    24,820
    cryptogirl
    Feb 25, 2006
  4. lrantisi
    Replies:
    2
    Views:
    619
    Karl Uppiano
    Mar 17, 2007
  5. Replies:
    4
    Views:
    184
    Anthony Jones
    Feb 14, 2006
Loading...

Share This Page