1

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?

  • Possible duplicate of [Converting a wstring to jstring on Linux](https://stackoverflow.com/questions/8272512/converting-a-wstring-to-jstring-on-linux) – Andrew Henle Jan 17 '18 at 21:43
  • the other solution is using iconv, i'm searching for a solution without it. –  Jan 18 '18 at 09:31

2 Answers2

3

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.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

The easy form is to convert your std::wstring to std::string to use the NewStringUTF.

Here you have a thread with the conversion How to convert wstring into string?