user4122560
user4122560

Reputation:

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?

Upvotes: 1

Views: 1825

Answers (2)

Alex Cohn
Alex Cohn

Reputation: 57173

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.

Upvotes: 3

ZaruWright
ZaruWright

Reputation: 1

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?

Upvotes: 0

Related Questions