Search code examples
c++stringjava-native-interfacecyrillic

Convert c++ string with cyrillic letters to jstring


I am trying to convert a c++ string with cyrillic russian letters to JNI jstring
But i getting different string in the output

My code that converts string to jstring:

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

Output in java:

ïðèâåò

How to do it right?


Solution

  • 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.