Home > Back-end >  How to convert c string with russian (cyrillic) letters to jstring
How to convert c string with russian (cyrillic) letters to jstring

Time:11-05

I am trying to convert a c string with russian letters to jni jstring
But in the output of the java program i getting a different string

How i converting:

const char* msg = "привет";
return env->NewStringUTF(msg);

This returns in java:

ïðèâåò

How to do it right?

CodePudding user response:

First, you have to make sure your input char* string is encoded in UTF-8 to begin with (which it isn't, in your example).

Second, JNI's NewStringUTF() method requires the input string to be encoded in modified UTF-8, not in standard UTF-8.

When dealing with non-ASCII chracters, you are better off using a UTF-16 encoded char16_t*/wchar_t* string with JNI's NewString() method instead.

  • Related