Java JNI String Conversions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.