07-17 16:09:04.060: A/libc(7467): Fatal signal 11 (SIGSEGV) at 0x0000002c (code=1)
底下两个函数都可以用于char* 转jstring:
jstring CStr2Jstring( JNIEnv* env, const char* pat )
{
//定义java String类 strClass
jclass strClass = (env)->FindClass("java/lang/String");
//获取java String类方法String(byte[],String)的构造器,用于将本地byte[]数组转换为一个新String
jmethodID ctorID = (env)->GetMethodID(strClass, "", "([BLjava/lang/String;)V");
//建立byte数组
jbyteArray bytes = (env)->NewByteArray((jsize)strlen(pat));
//将char* 转换为byte数组
(env)->SetByteArrayRegion(bytes, 0, (jsize)strlen(pat), (jbyte*)pat);
//设置String, 保存语言类型,用于byte数组转换至String时的参数
jstring encoding = (env)->NewStringUTF("GB2312");
//将byte数组转换为java String,并输出
return (jstring)(env)->NewObject(strClass, ctorID, bytes, encoding);
}
jstring chars2Jstring( JNIEnv* env, const char* pat )
{
//定义java String类 strClass
JniMethodInfo class_init;
//获取类的构造函数
if (JniHelper::getMethodInfo(class_init,"java/lang/String","","([BLjava/lang/String;)V") == false)
{
CCLog("JniHelper::FindClass failed");
return nullptr;
}
//建立byte数组
jbyteArray bytes = (env)->NewByteArray((jsize)strlen(pat));
//将char* 转换为byte数组
(env)->SetByteArrayRegion(bytes, 0, (jsize)strlen(pat), (jbyte*)pat);
//设置String, 保存语言类型,用于byte数组转换至String时的参数
jstring encoding = (env)->NewStringUTF("GB2312");
//将byte数组转换为java String,并输出
return (jstring)(env)->NewObject(class_init.classID, class_init.methodID, bytes, encoding);
}
char* jstringTostring(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("utf-8");
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
jsize alen = env->GetArrayLength(barr);
jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
if (alen > 0)
{
rtn = (char*)malloc(alen + 1);
memcpy(rtn, ba, alen);
rtn[alen] = 0;
}
env->ReleaseByteArrayElements(barr, ba, 0);
return rtn;
}