[英]jstring(JNI) to std::string(c++) with utf8 characters
How to convert jstring
(JNI) to std::string
(c++) with utf8
characters?如何使用
utf8
字符将jstring
(JNI) 转换为std::string
(c++)?
this is my code.这是我的代码。 it worked with non-utf8 characters, but it is wrong with utf8 characters.
它适用于非 utf8 字符,但使用 utf8 字符是错误的。
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:
我找到了一种方法:
In java, a unicode char will be encoded using 2 bytes ( utf16
).在 java 中,unicode char 将使用 2 个字节(
utf16
)进行编码。 so jstring
will container characters utf16
.所以
jstring
将容器字符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. c++ 中的
std::string
本质上是一个字节串,而不是字符,所以如果我们想将jstring
从JNI
传递给c++
,我们必须将utf16
转换为字节。
in document JNI functions , we have 2 functions to get string from jstring:在文档JNI 函数中,我们有 2 个函数可以从 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
, it will return a modified utf8 . GetStringUTFChars
,它将返回修改后的 utf8 。
GetStringChars
will return jbyte *, we will read char code from jbytes and convert it to char in c++ GetStringChars
将返回 jbyte *,我们将从 jbytes 中读取字符代码并将其转换为 C++ 中的字符
this is my solution (worked well with ascii
and utf8
characters):这是我的解决方案(适用于
ascii
和utf8
字符):
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;
}
jboolean isCopy;
const char *convertedValue = (env)->GetStringUTFChars(yourJStringParam, &isCopy);
std::string string = std::string(convertedValue, length)
This works just fine.这工作得很好。 Give it a try.
试一试。
explanation is here:解释在这里:
http://www.club.cc.cmu.edu/~cmccabe/blog_jni_flaws.html https://developer.android.com/training/articles/perf-jni (Region calls) http://www.club.cc.cmu.edu/~cmccabe/blog_jni_flaws.html https://developer.android.com/training/articles/perf-jni (区域调用)
jsize
string_j2c(JNIEnv *env, jstring p, char *buffer) {
if (buffer != NULL) {
// Returns the length (the count of Unicode characters) of a
// Java string.
const jsize len = (*env).GetStringLength(p);
// Translates 'len' number of Unicode characters into modified
// UTF-8 encoding and place the result in the given buffer.
(*env).GetStringUTFRegion(p, 0, len, buffer);
// Returns JNI_TRUE when there is a pending exception;
// otherwise, returns JNI_FALSE.
const jboolean is_error = (*env).ExceptionCheck();
if (is_error == JNI_TRUE) {
return -1;
}
}
// Returns the length in bytes of the modified UTF-8
// representation of a string.
const jsize len = (*env).GetStringUTFLength(p);
return len;
}
const jsize len = string_j2c(env, p, NULL);
char buffer[len];
const jsize ret = string_j2c(env, p, buffer);
if (ret == -1) { // error
}
else {
__android_log_print(ANDROID_LOG_DEBUG, "Native", "%s", buffer);
}
I tweaked @Nikhil Tambe 's answer to look like this:我调整了@Nikhil Tambe的答案,看起来像这样:
jboolean isCopy;
const char *convertedValue = (env)->GetStringUTFChars(yourJStringParam, &isCopy);
std::string str = convertedValue;
And that worked!那奏效了! Because in Standard C++, the type of a string literal is
const char *
and therefore you can assign const char *convertedValue
to a variable of type std::string
.因为在标准 C++ 中,字符串文字的类型是
const char *
,因此您可以将const char *convertedValue
分配给std::string
类型的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.