Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Passing Unicode string to Java from Native function

843829Jun 29 2007 — edited Jul 5 2007
I have a native function that returns a Unicode string, which I use the NewString() jni method to return it. My native string is in UTF16 LE for Windows and BE for Unix (platform default encoding). Currently I am only using the ASCII characters that are represented in UTF16. When I get a string in Java, the byte array of the string is indeed UTF16LE or BE depending on the platform. However, when I try to use this string on GUI, it shows all spaces on Unix (BE).

Here is what I tried:

// get the string from my native method
String strUTF16 = getMyString();

// get the byte array
byte[] byteArray = strUTF16.getBytes();

// According to the Java reference, this should construct a new String
// by decoding the specified array of bytes using the platform's default charset,
// but it does not seem to be working. WHY???
String str1 = new String (byteArray);

// this works on unix
String str2 = new String (byteArray, "UTF-16BE");

My question is, why is my first try to construct a string not working? If I have to specify the encoding as in my second try, how can I detect the platform default encoding? I do not want to hardcode the encoding if I don't have to...

Thanks much.

Nikki
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 2 2007
Added on Jun 29 2007
5 comments
383 views