带有 utf8 字符的 jstring(JNI) 到 std::string(c++)

jstring(JNI) to std::string(c++) with utf8 characters(带有 utf8 字符的 jstring(JNI) 到 std::string(c++))
本文介绍了带有 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 本质上是一个字节串,而不是字符,所以如果我们想将 jstringJNI 传递到 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++

这是我的解决方案(适用于 asciiutf8 字符):

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++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)