84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
java的实体里定义的char[] name JNI定义结构体属性char[] name将结构体属性值(中文)传给java是乱码,怎么解决?不要让我改变属性类型!
人生最曼妙的风景,竟是内心的淡定与从容!
Please refer to this article
You must first figure out what the codes are on both sides.
The char[] string in C is UTF-8 encoded by default. You can use the NewStringUTF() function of JNIEnv to convert the char[] string in C to a Java string. The signature of this function is:
jstring NewStringUTF(const char* bytes)
Look at the simplest example:
JNIEXPORT jstring JNICALL Java_com_example_gnaix_ndk_NativeMethod_getString (JNIEnv *env, jclass object, jstring str){ //1. 将unicode编码的java字符串转换成C风格字符串 const char *buf_name = env->GetStringUTFChars(str, 0); if(buf_name == NULL){ return NULL; } int len = strlen(buf_name); char n_name[len]; strcpy(n_name, buf_name); //2. 释放内存 env->ReleaseStringUTFChars(str, buf_name); //3. 处理 n_name="ro.serialno" char buf[1024]; __system_property_get(n_name, buf); LOGD("serialno : %s", buf); //4. 去掉尾部"rrreee" int len_buf = strlen(buf); string result(buf, len_buf); return env->NewStringUTF(result.c_str()); }
Please refer to this article
You must first figure out what the codes are on both sides.
The char[] string in C is UTF-8 encoded by default. You can use the NewStringUTF() function of JNIEnv to convert the char[] string in C to a Java string. The signature of this function is:
Look at the simplest example: