Encoding diacritics string (non ASCII) in NDK to Java and back

585 views
Skip to first unread message

Stefan

unread,
Jan 29, 2012, 12:44:03 PM1/29/12
to android-ndk
Hi,

I have a problem with encoding characters, especially with diacritical
marks. My old C++ code is using Slovak characters like š, č, ž, ť, ú,
ä and so on (Windows-1250 or ISO 8859-2 charset).

In C++ code I use char* and wchar_t* types. My builded code is running
on Windows, Unix (HP-UX) and Windows Mobile (PocketPC). To correct
conversion it is setlocale() and wcstombs() function called. In
Windows it is setlocale(LC_ALL, ".1250"). In HP-UX
setlocale(LC_ALL,"sk_SK.utf8") and so on. However in Android I tried
to set different parameters to setlocale(), but nothing is working.

wchar_t* strw = L"Alekšince";
char str[100];
wcstombs(str, strw, 100); // in "str" is not correct characters,
because setlocale() is not working

My first question is - what default encoding is using in wchar_t in
NDK, if I call setlocale(LC_ALL, "C")?

NDK does not support any encoding obviously or I do not know what
values I have to pass in setlocale(). One solution is realize
conversion in Java, but it is not working too.

Finally, Java is using Unicode Strings. From NDK to Java is used
jstring, for example obtained by env->NewStringUTF("some string");
This function expects string in UTF-8, but I guess that my string is
not in UTF-8. So next question - how convert my string to UTF-8 in
order to call env->NewStringUTF(). I mean that it would be work or
not? :)

I tried to use jbyteArray instead of jstring, but it is not working
too.

Thanks for any advice.

Stefan

Elliott Hughes

unread,
Jan 30, 2012, 2:02:21 PM1/30/12
to android-ndk
the easiest is to just use UTF-8 throughout your code. alternatively,
you can use NewString, which takes an array of jchar instead.

> I tried to use jbyteArray instead of jstring, but it is not working
> too.

i think this might be the easiest route if you're stuck with legacy
code using ISO-8859-2 (or any other non-UTF-8 encoding) --- pass a
jbyteArray back up to managed code, and then use the appropriate
String constructor on the managed side:
http://developer.android.com/reference/java/lang/String.html#String(byte[],
java.lang.String)

you can of course use JNI to invoke that constructor yourself and pass
the resulting jstring back up to managed code, but that's more native
code, which is a thing to be avoided.
Reply all
Reply to author
Forward
0 new messages