Search code examples
c++java-native-interfacewstring

convert to jstring from std::wstring


I'm trying to convert a wstring from my cpp code to jstring.

I know there's a method named NewStringUTF to get jstring from std::string (after getting the const char* from that string of course).

is there a way I can do the same if the input is std::wstring without using iconv?


Solution

  • JNI has NewString(JNIEnv * , const jchar * , jsize) function. If your std::wstring::c_str() returns a pointer compatible with jchar* (this depends on your platform), you can try this function. Note that if your wchar_t is 32 bit (as on Linux), you cannot use it directly. See Android JNI - Reliable way to convert jstring to wchar_t and How do I convert jstring to wchar_t * for more info.