jstring(JNI) to std::string(c++) with utf8 characters(带有 utf8 字符的 jstring(JNI) 到 std::string(c++))
问题描述
如何将 jstring (JNI) 转换为 std::string (c++) 与 utf8 字符?
How to convert jstring (JNI) to std::string (c++) with utf8 characters?
这是我的代码.它适用于非 utf8 字符,但适用于 utf8 字符.
this is my code. it worked with non-utf8 characters, but it is wrong with utf8 characters.
std::string jstring2string(JNIEnv *env, jstring jStr){
const char *cstr = env->GetStringUTFChars(jStr, NULL);
std::string str = std::string(cstr);
env->ReleaseStringUTFChars(jStr, str);
return str;
}
推荐答案
经过很长时间才找到解决方案.我找到了一条路:
After a lot time to find solution. i was found a way:
在 java 中,unicode char 将使用 2 个字节 (utf16) 进行编码.所以 jstring 将包含字符 utf16.c++ 中的 std::string 本质上是一个字节串,而不是字符,所以如果我们想将 jstring 从 JNI 传递到 c++,我们已经将 utf16 转换为字节.
In java, a unicode char will be encoded using 2 bytes (utf16). so jstring will container characters utf16. std::string in c++ is essentially a string of bytes, not characters, so if we want to pass jstring from JNI to c++, we have convert utf16 to bytes.
在文档中 JNI 函数,我们有 2 个函数从 jstring 中获取字符串:
in document JNI functions, we have 2 functions to get string from jstring:
// Returns a pointer to the array of Unicode characters of the string.
// This pointer is valid until ReleaseStringchars() is called.
const jchar * GetStringChars(JNIEnv *env, jstring string, jboolean *isCopy);
// Returns a pointer to an array of bytes representing the string
// in modified UTF-8 encoding. This array is valid until it is released
// by ReleaseStringUTFChars().
const char * GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy);
GetStringUTFChars,它将返回一个修改后的utf8.
GetStringChars 将返回 jbyte *,我们将从 jbytes 中读取 char 代码并在 c++ 中将其转换为 char
GetStringChars will return jbyte *, we will read char code from jbytes and convert it to char in c++
这是我的解决方案(适用于 ascii 和 utf8 字符):
this is my solution (worked well with ascii and utf8 characters):
std::string jstring2string(JNIEnv *env, jstring jStr) {
if (!jStr)
return "";
const jclass stringClass = env->GetObjectClass(jStr);
const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));
size_t length = (size_t) env->GetArrayLength(stringJbytes);
jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL);
std::string ret = std::string((char *)pBytes, length);
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
env->DeleteLocalRef(stringJbytes);
env->DeleteLocalRef(stringClass);
return ret;
}
这篇关于带有 utf8 字符的 jstring(JNI) 到 std::string(c++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 utf8 字符的 jstring(JNI) 到 std::string(c++)
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
