The NDK development string operation

Source: Internet
Author: User
Tags java se

In Jni, the Java string is treated as a reference. These reference types are not directly used as native C strings, and JNI provides the necessary functions for converting between Java strings and C strings, because Java String objects are immutable (if you disagree, review Java SE), Therefore, JNI does not provide any functions that modify the contents of existing Java strings.

JNI supports two encoded-format strings, Unicode and UTF-8, respectively.

All right, the crap is over.

First, create a string

We can use the NewString function in native C or C + + code to construct a Unicode encoded string, or you can use the NEWSTRINGUTF function to construct a string in UTF-8 format, such that a Java string is constructed with a given C string.

jstring Java_com_example_jni_MainActivity_stringTest1(JNIEnv* env, jobject thiz) {//创建字符串,用给定的C字符串创建Java字符串    jstring javaString;    "hello world!,hello lenve");}

B. Convert Java string to C string

In order to use Java strings in native code, you need to first convert the Java string to a C string, and use the Getstringchars function to convert a Unicode-formatted Java string into a C string. Use the Getstringutfchars function to convert a Java string in UTF-8 format into a C string. The third parameter of both functions is an optional parameter, which is Iscopy, which allows the caller to determine whether the returned C-string address points to a replica or to a fixed object in the heap.

Jstring Java_com_example_jni_mainactivity_stringtest2 (jnienv* env, Jobject thiz, jstring javaString) {//Convert Java string to C stringConstjbyte* str;    Jboolean iscopy; Jstring Javastr; str = (*env)Getstringutfchars (env,javastring,&iscopy);if(0!=STR) {printf ("Java String is:%s", str);if(jni_true==iscopy) {printf ("C string is a copy of the Java string."); Javastr = (*env)Newstringutf (ENV,"C string is a copy of the Java string."); }Else{printf ("C string points to actual string."); Javastr = (*env)Newstringutf (ENV,"C string points to actual string."); }    }returnJavastr;}

Third, release the string

The C string obtained through the Getstringchars function and the Getstringutfchars function is released correctly after use in the native code, otherwise it will cause a memory leak. As with the above, there are two release functions. Releasestringutfchars and Releasestringchars, respectively, release strings in UTF-8 format and Unicode format.

The function has three parameters, the first one in the previous blog has said, not much, the second and third represents the Java string and the C string respectively

This project source code download

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

The NDK development string operation

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.