How to convert char [] to jstring in JNI?

In jni, I want to convert char * to jstring using the following method:

env->NewStringUTF(chm_pcText) 

it works for English text, but not Chinese, and receives the following message:

 JNI WARNING: illegal continuation byte. 

How to solve it?

+7
source share
2 answers

I just solve this: two steps: first convert the char * to jbyteArray, then call the java String contructor to create the jstring.

  strClass = global_env->FindClass("java/lang/String"); ctorID = global_env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V"); encoding = global_env->NewStringUTF("GBK"); jbyteArray bytes = global_env->NewByteArray(strlen(chm_pcText)); global_env->SetByteArrayRegion(bytes, 0, strlen(chm_pcText), (jbyte*)chm_pcText); jstring str = (jstring)global_env->NewObject(strClass, ctorID, bytes, encoding); 
+15
source

NewStringUTF worked for me. Here is the code that worked for me:

 char *returnString = (char*)malloc(10); strcpy(returnString, "电脑"); return (*jnienv)->NewStringUTF(jnienv, returnString); 
+1
source

All Articles