JNI中类型转换
1.将jstring转char*
char* converjstringtochar(JNIEnv *env, jstring res) {
return (char*) env->GetStringUTFChars(res, 0);}
2.将char*转jstring
jstring converchartojstring(JNIEnv *env, char* res) {if(res == NULL)
{
res = "";
}
jclass strClass = env->FindClass("java/lang/String");
jmethodID ctorID = env->GetMethodID(strClass, "<init>","([BLjava/lang/String;)V");
jbyteArray bytes = env->NewByteArray(strlen(res));
env->SetByteArrayRegion(bytes, 0, strlen(res), (jbyte*) res);
jstring encoding = env->NewStringUTF("utf-8");
return (jstring) env->NewObject(strClass, ctorID, bytes, encoding);
}
3.将char*装jbyteArray
jbyteArray converchartojbyteArray(JNIEnv *env, char* res,int size)
{
jbyteArray bytearray = env->NewByteArray(size);
env->SetByteArrayRegion(bytearray, 0, size, (jbyte*) res);
return bytearray;
}