How to convert between Android JNI and Java data transfer

Source: Internet
Author: User

The jstring data type in 1.c is the Java incoming string object, and the conversion of the JNI function can become the char* of C.
java local C type description
boolean jboolean unsigned 8 bit
byte jbyte unsigned 8 bit
char jchar unsigned 16 bit
short jshort signed 16 bit
int jint signed 32 bit
long jlong signed 64 bit
float jfloat signed 32 bit
double jdouble signed 64 bit
2. How do I convert a Java-passed string parameter to C's char* and then use the? Java passed in the string parameter, in the C file by Jni converted to jstring data type, in the C file declared char* test, and thenTest = (char*) (*env)->getstringutfchars (env, jstring, NULL); Note: When test is finished, notify the virtual machine platform that the relevant code is no longer accessible:(*env)->releasestringutfchars (env, jstring, test);3. Pass a char* buffer obtained in C to Java?     This char* if it is a generic string, it can be passed back as a string. If it is a buffer containing '/0 ', it is best to pass as ByteArray because the length of the copy can be set, and if copy is to string, it may be truncated to '/0 '. There are two ways to pass the resulting data: a byte array that is directly new in JNI and then called Functions     (*env)->setbytearrayregion (env, ByteArray, 0, Len, Buffer), copy the value of buffer to ByteArray, and the function will return directly to ByteArray. One is the return error number, the data is passed as a parameter, but the basic data type of Java is a value, and the object is a passing reference, so the byte array that needs to be outgoing is wrapped with a class, as follows:    class retobj{        public byte[] bytearray;   } This object is retobj outgoing as a function parameter The assignment of a byte array in Retobj is facilitated by the following function. The code is as follows:    Jclass cls;    jfieldid fid;    jbyteArray bytearray;     ByteArray = (*env)->newbytearray (Env,len);    (*env)->setbytearrayregion (env, ByteArray, 0, len, buffer);    CLS = (*env)->getobjectclass (env, retobj);    FID = (* ENV)->getfieldid (env, CLS, "Retbytes", "[B"]);    (*env)->setobjectField (env, retobj, FID, ByteArray); 4. How to return a string in C to java    Char charstr[50];    Jstring jstr;  jstr = env-Newstringutf (CHARSTR);5. How much space does not know how to use buffer, how to pass out? In the JNI C file, new out of space, passed out. Java data is not initialized, pointing to the space passed out. Another: the processing of Java incoming data in JNI if the incoming is ByteArray, the following processing gets Buffer:char *tmpdata = (char*) (*env)->getbytearrayelements (env, byte    Array, NULL); (*env)->releasebytearrayelements (env, ByteArray, Tmpdata, 0);

How to convert between Android JNI and Java data transfer

Related Article

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.