In jni, I want convert the char * to jstring using following method:
env->NewStringUTF(chm_pcText)
it works for english text, but failed with Chinese, and get following message:
JNI WARNING: illegal continuation byte.
How to solve it ?
I just solve it: two steps, first: convert the char * to jbyteArray, then call java String's contructor to generate a 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);
NewStringUTF worked for me. Here is code that worked for me:
char *returnString = (char*)malloc(10);
strcpy(returnString, "电脑");
return (*jnienv)->NewStringUTF(jnienv, returnString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With