Android transfers utf8 characters from JNI to Java

Source: Internet
Author: User

The problem of garbled characters passing in utf8 from JNI has plagued me for a long time. Today, I finally found a solution.

Since env-> newstringutf supports modified UTF-8, this API cannot be used from JNI

To Java.

// Returnutf8.java </P> <p> View plaincopy to clipboardprint? </P> <p> public class returnutf8 {</P> <p> static {</P> <p> system. loadlibrary ("returnutf8jni"); </P> <p >}</P> <p> Public static native byte [] Method1 (); </P> <p> Public static native object method2 (); </P> <p> Public static void main (string [] ARGs) {</P> <p> try {</P> <p> byte [] buffer = returnutf8.method1 (); </P> <p> string S1 = new string (buffer, "UTF-8"); </P> <p> system. out. println ("S1 =" + S1); </P> <p> string S2 = returnutf8.method2 (). tostring (); </P> <p> system. out. println ("S2 =" + S2); </P> <p >}catch (Java. io. unsupportedencodingexception e) {</P> <p> E. printstacktrace (); </P> <p >}</P> <p> public class </P> <p> returnutf8 {</P> <p> static {</P> <p> system. loadlibrary ("returnutf8jni"); </P> <p >}</P> <p> Public static native byte [] Method1 (); </P> <p> Public static native object method2 (); </P> <p> Public static void main (string [] ARGs) {</P> <p> try {</P> <p> byte [] buffer = returnutf8.method1 (); </P> <p> string S1 = new string (buffer, "UTF-8"); </P> <p> system. out. println ("S1 =" + S1); </P> <p> string S2 = returnutf8.method2 (). tostring (); </P> <p> system. out. println ("S2 =" + S2); </P> <p >}catch (Java. io. unsupportedencodingexception e) {</P> <p> E. printstacktrace (); </P> <p >}</P> <p> </P> <p>

 

# Javac returnutf8.java

 

 

 

 

 

# Java returnutf8

 

  

A returnutf8.h header file will be generated here

 

 

 

// Returnutf8.cpp </P> <p> View plaincopy to clipboardprint? </P> <p> # include "returnutf8.h" </P> <p> jniexport jbytearray jnicall java_returnutf8_method1 </P> <p> (jnienv * ENV, jclass CLs) </P> <p >{</P> <p> jbyte buffer [] =/* utf8 encoding buffer */</P> <p> jbytearray bytes = env-> newbytearray (sizeof (buffer )); </P> <p> env-> setbytearrayregion (bytes, 0, sizeof (buffer), buffer); </P> <p> return bytes; </P> <p >}</P> <p> jniexport jbytearray jnicall java_returnutf8_method2 </P> <p> (jnienv * ENV, jclass CLs) </P> <p >{</P> <p> jbyte buffer [] =/* utf8 encoding buffer */</P> <p> jbytearray bytes = env-> newbytearray (sizeof (buffer )); </P> <p> env-> setbytearrayregion (bytes, 0, sizeof (buffer), buffer ); </P> <p> jchar encoding_name [] = {'U', 't', 'F', '-', '8 ',''}; </P> <p> jstring encoding = env-> newstring (encoding_name, 5 ); </P> <p> jclass thestringclass = env-> findclass ("ljava/lang/string ;"); </P> <p> jmethodid mid = env-> getmethodid (thestringclass, "", "([bljava/lang/string;) V "); </P> <p> jobject result = env-> newobject (thestringclass, mid, bytes, encoding); </P> <p> return result; </P> <p >}</P> <p> # include </P> <p> "returnuf8.h" </P> <p> jniexport jbytearray jnicall java_returnuf8_method1 </ p> <p> (jnienv * ENV, jclass CLs) </P> <p >{</P> <p> jbyte buffer [] =/* utf8 encoding buffer */</P> <p> jbytearray bytes = env-> newbytearray (sizeof (buffer )); </P> <p> env-> setbytearrayregion (bytes, 0, sizeof (buffer), buffer); </P> <p> return bytes; </P> <p >}</P> <p> jniexport jbytearray jnicall java_returnutf8_method2 </P> <p> (jnienv * ENV, jclass CLs) </P> <p >{</P> <p> jbyte buffer [] =/* utf8 encoding buffer */</P> <p> jbytearray bytes = env-> newbytearray (sizeof (buffer )); </P> <p> env-> setbytearrayregion (bytes, 0, sizeof (buffer), buffer ); </P> <p> jchar encoding_name [] = {'U', 't', 'F', '-', '8 ',''}; </P> <p> jstring encoding = env-> newstring (encoding_name, 5 ); </P> <p> jclass thestringclass = env-> findclass ("ljava/lang/string ;"); </P> <p> jmethodid mid = env-> getmethodid (thestringclass, "", </P> <p> "([bljava/lang/string ;) V "); </P> <p> jobject result = env-> newobject (thestringclass, mid, bytes, </P> <p> encoding ); </P> <p> return result; </P> <p >}</P> <p>

 

# G ++-shared-I/usr/lib/JVM/Java-1.5.0-sun/include-I/usr/lib/JVM/Java-1.5.0-sun/include/Linux returnutf8.cpp-O libreturnutf8jni. so

 

 

# Export ld_library =.

# Java returnutf8

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.