Home arrow static arrow Java Programming [Archive] - String to bytes on linux
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - String to bytes on linux
This topic has 3 replies on 1 page.

Posts:79
Registered: 2/6/04
String to bytes on linux  
Aug 8, 2004 11:30 AM



 
Hi everyone!

I have just ran a program on my linux (which worked fine on win). it gave me an exception of an "ArrayOutOfBoundsException" when I looked at the code it seems that it translates strings whose length is 8 to byte arrays of 15 or 16 using the getBytes method in the String class.

why is that ?
is that method platform-dependent ?

thanks,
Shlomi
 

Posts:18,384
Registered: 21.03.00
Re: String to bytes on linux  
Aug 8, 2004 11:47 AM (reply 1 of 3)



 
Hi,

Strings are unicode, so one character migh be represented with more than one byte.

/Kaj
 

Posts:11,200
Registered: 7/22/99
Re: String to bytes on linux  
Aug 8, 2004 12:25 PM (reply 2 of 3)



 
is that method platform-dependent ?

Yes, like it says in the API specification: "Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array."
(http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#getBytes())

The platform's default character encoding naturally depends on the platform. On Windows in the western world it's usually windows-1252, on Linux (and Unix in general) it depends on the locale settings (use the "locale" command to find out). If you need to convert the string to bytes using a specific encoding you should use the overload that accepts the name of the encoding as a paramter: String.getBytes(String encoding)
 

Posts:79
Registered: 2/6/04
Re: String to bytes on linux  
Aug 8, 2004 12:32 PM (reply 3 of 3)



 
thank you both.
 
This topic has 3 replies on 1 page.