简体   繁体   English

将jchararray分配给const unsigned char

[英]assign jchararray to const unsigned char

I pass a char [] from java to jni : 我从java传递了一个char []到jni:

function(char [] aChar);

then I read the char [] at jni : 然后我在jni读了char []:

JNIEXPORT jbyteArray JNICALL
packagename_function(JNIEnv *env, jobject obj, jcharArray aChar) {

  const unsigned char *theValue;
}

Now I want to assign aChar as a value for theValue. 现在,我想将aChar分配为TheValue的值。

What is the correct way to assign it? 正确的分配方式是什么?

You have two options here: 您在这里有两个选择:

  1. You can pass array (as you do) and then, you need to retrieve it's elements: 您可以传递数组(如您所愿),然后需要检索它的元素:

    http://jnicookbook.owsiak.org/recipe-No-012/ http://jnicookbook.owsiak.org/recipe-No-012/

     /* get size of the array */ jsize len = (*env)->GetArrayLength(env, array); /* get the body of array; it will be referecende by C pointer */ jchar *body = (*env)->GetCharArrayElements(env, array, 0); /* do some stuff */ for(int i=0; i < len; i++) { printf("Char value: %c\\n", body[i]); } /* release body when you decide it is no longer needed */ (*env)->ReleaseCharArrayElements(env, array, body, 0); 
  2. You can pass characters as a String object 您可以将字符作为String对象传递

    http://jnicookbook.owsiak.org/recipe-No-009/ http://jnicookbook.owsiak.org/recipe-No-009/

     // we have to get string bytes into C string const char *c_str; c_str = (*env)->GetStringUTFChars(env, str, NULL); if(c_str == NULL) { return; } printf("Passed string: %s\\n", c_str); // after using it, remember to release the memory (*env)->ReleaseStringUTFChars(env, str, c_str); 

    In case of passing String from Java to C you have to change two things: 如果将String从Java传递到C,则必须更改两件事:

    • You need to change your method signature to 您需要将方法签名更改为

       packagename_function(JNIEnv *env, jobject obj, jstring aChar) 
    • In your Java code, you have to create String from characters 在Java代码中,您必须根据字符创建String

       char data[] = {'a', 'b', 'c'}; String str = new String(data); 

    and then, pass it to native code. 然后将其传递给本机代码。

    Note! 注意! Be careful with types lengths! 注意类型长度! You can always check here: 您可以随时在这里查看:

    https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html

In java side, the char is encoding with UTF16, is counter part jni side is unsigned short not unsigned char, so i think if you want to set the java side char array to jni side, you have two options: 1. In java, convert the char array to byte array with UTF8 encoding. 在Java端,char用UTF16编码,是计数器部分jni端是unsigned short而不是unsigned char,所以我认为如果要将Java端char数组设置为jni端,则有两个选择:1.在Java中,使用UTF8编码将char数组转换为byte数组。 2. Or in jni side, convert the UTF16 encoding string to UTF8 string. 2.或者在jni端,将UTF16编码字符串转换为UTF8字符串。 I think the previous option is more convenient. 我认为以前的选择更方便。

Thanks to @mko for giving me the idea. 感谢@mko给我这个主意。 My final code is : 我的最终代码是:

/* get size of the array */
jsize len = (*env)->GetArrayLength(env, aChar);

/* get the body of array; it will be referecende by C pointer */
jchar *body = (*env)->GetCharArrayElements(env, aChar, 0);

jstring mystr =  (*env)->NewString(env, body, len);

/* release body when you decide it is no longer needed */
(*env)->ReleaseCharArrayElements(env, aChar, body, 0);

const unsigned char *theValue = (*env)->GetStringUTFChars(env,mystr, NULL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM