Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Converting String to byte array

Thread view: 
Raga - 05 Oct 2006 16:01 GMT
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[i]);
           b[(i * 2) + 1] = (byte)(c[i] >> 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 - 05 Oct 2006 16:03 GMT
> Hi,
>
[quoted text clipped - 22 lines]
> 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.
Oliver Wong - 05 Oct 2006 16:13 GMT
> Hi,
>
[quoted text clipped - 24 lines]
>
> 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
Thomas Fritsch - 05 Oct 2006 16:28 GMT
>>Hi,
>>
[quoted text clipped - 17 lines]
>>
>>"
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");
    }

Signature

Thomas



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.