1, jstring turn qstring
For Qt5.2 above (including) can be used qandroidjniobject::tostring (), see here: https://stackoverflow.com/questions/27345570/convert-jstring-to-qstring
The lower version of QT will be converted by itself, and there are two ways to get jstring in JNI:
getstringutfchars--get ( improved ) UTF8 encoded string
getstringchars--Gets the Unicode encoded string, which is recommended because Java is a Unicode encoding, which reduces the one-time encoding conversion
1Jboolean iscopy =Jni_false;2 Constjchar* value = Env->getstringchars (Jstringvalue, &iscopy);3 if(Value! =NULL)4 {5QString Qsvalue =qstring::fromutf16 (value);6 if(iscopy)7 {8Env->releasestringchars (jstringvalue, value);9 }Ten}
Also about Getstringutfchars return is ( improved) UTF8 encoded string , can refer to this article: https://blog.wsxyeah.xyz/2016/07/23/jni-charset-encoding/
2. Jstring to a BSTR
Because a BSTR is Unicode encoding, it is convenient to convert with Getstringchars:
1Jboolean iscopy =Jni_false;2 Constjchar* paramvalue = Env->getstringchars (Jstrparam, &iscopy);3 if(Paramvalue! =NULL)4 {5BSTR BSTR = SysAllocString (paramvalue);//after use, remember to release: SysFreeString (BSTR);6 if(iscopy)7 {8Env->Releasestringchars (Jstrparam, paramvalue);9 }Ten}
Java JNI String Conversions