Android ndk中字符串转utf-8和gbk

转utf-8
jstring cToJstringutf(JNIEnv* env, const char* pat) {
	jclass strClass = (*env)->FindClass(env, "java/lang/String");
	jmethodID ctorID = (*env)->GetMethodID(env, strClass, "",
			"([BLjava/lang/String;)V");
	jbyteArray bytes = (*env)->NewByteArray(env, strlen(pat));
	(*env)->SetByteArrayRegion(env, bytes, 0, strlen(pat), (jbyte*) pat);
	jstring encoding = (*env)->NewStringUTF(env, "utf-8");
	return (jstring)(*env)->NewObject(env, strClass, ctorID, bytes, encoding);
}
转gbk
jstring cTojstringbk(JNIEnv* env, char* pat) {
	jclass strClass = (*env)->FindClass(env, "java/lang/String");
	jmethodID ctorID = (*env)->GetMethodID(env, strClass, "",
			"([BLjava/lang/String;)V");
	jbyteArray bytes = (*env)->NewByteArray(env, strlen(pat));
	(*env)->SetByteArrayRegion(env, bytes, 0, strlen(pat), (jbyte*) pat);
	jstring encoding = (*env)->NewStringUTF(env, "gbk");
	return (jstring)(*env)->NewObject(env, strClass, ctorID, bytes, encoding);
}

若转其他字符集类似。

你可能感兴趣的:(android)