UTF 16 (byte[]) <--> String?  
Author Message
Ong Hong Peow





PostPosted: 2004-9-1 12:14:00 Top

java-programmer, UTF 16 (byte[]) <--> String? Hi,

is there a convenient way to convert unicode that's stored in byte array to
java String?
and vice-versa?

e.g. for a single single unicode,
the higher byte would be in byte[0],
lower byte[1]
and byte[2], byte[3] = 0. null terminated

the byte array of unicode is generated from a C program and transfered over
the network ..

looking over the String API doesn't reveal much ... or am I overlooking
something?

Thanks in advance.


 
Mike Schilling





PostPosted: 2004-9-1 12:42:00 Top

java-programmer >> UTF 16 (byte[]) <--> String?
"Ong Hong Peow" <email***@***.com> wrote in message
news:ch3gq0$foo$email***@***.com...
> Hi,
>
> is there a convenient way to convert unicode that's stored in byte array
to
> java String?
> and vice-versa?
>
> e.g. for a single single unicode,
> the higher byte would be in byte[0],
> lower byte[1]
> and byte[2], byte[3] = 0. null terminated
>
> the byte array of unicode is generated from a C program and transfered
over
> the network ..
>
> looking over the String API doesn't reveal much ... or am I overlooking
> something?
>
> Thanks in advance.
>

String -> bytes: string.getBytes("UTF-16BE")
bytes -> String: new String(bytes, 0, len, "UTF-16BE")

You'd have to write code to find the length of the byte array (i.e. to
search for the null terminator.)


 
Ong Hong Peow





PostPosted: 2004-9-1 14:43:00 Top

java-programmer >> UTF 16 (byte[]) <--> String? Thanks Mike.

where do one find those available "charsetName" in getBytes(String
charsetName) ?

Thanks again ;-)

"Mike Schilling" <email***@***.com> wrote in message
news:GqcZc.13983$email***@***.com...
>
> "Ong Hong Peow" <email***@***.com> wrote in message
> news:ch3gq0$foo$email***@***.com...
> > Hi,
> >
> > is there a convenient way to convert unicode that's stored in byte array
> to
> > java String?
> > and vice-versa?
> >
> > e.g. for a single single unicode,
> > the higher byte would be in byte[0],
> > lower byte[1]
> > and byte[2], byte[3] = 0. null terminated
> >
> > the byte array of unicode is generated from a C program and transfered
> over
> > the network ..
> >
> > looking over the String API doesn't reveal much ... or am I overlooking
> > something?
> >
> > Thanks in advance.
> >
>
> String -> bytes: string.getBytes("UTF-16BE")
> bytes -> String: new String(bytes, 0, len, "UTF-16BE")
>
> You'd have to write code to find the length of the byte array (i.e. to
> search for the null terminator.)
>
>


 
 
Thomas Fritsch





PostPosted: 2004-9-1 20:05:00 Top

java-programmer >> UTF 16 (byte[]) <--> String? Ong Hong Peow wrote:

>...
>
>where do one find those available "charsetName" in getBytes(String
>charsetName) ?
>
>...
>
>
See http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html
There you find the short list of standard charsets (the minimum set to
be supported by any Java platform).
The complete list of available charsets can be got by calling
Charset.availableCharsets()

--
Thomas<dot>Fritsch<squiggle>ops<dot>de