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

时间:2017-01-24 04:29:50

标签: android c++ c++11 java-native-interface share

如何使用jstring字符将std::string(JNI)转换为utf8(c ++)?

这是我的代码。它使用非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;
}

3 个答案:

答案 0 :(得分:14)

经过很长时间才找到解决方案。我找到了一个方法:

在java中,unicode char将使用2个字节(utf16)进行编码。所以jstring将容器字符utf16。 c ++中的std::string本质上是一个字节字符串,而不是字符,所以如果我们想将jstringJNI传递给c++,我们将utf16转换为字节。

在文档JNI functions中,我们有两个函数来从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,它会返回modified utf8

GetStringChars将返回jbyte *,我们将从jbytes读取char代码并将其转换为c ++中的char

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

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;
}

答案 1 :(得分:2)

jboolean isCopy;
const char *convertedValue = (env)->GetStringUTFChars(yourJStringParam, &isCopy);

这很好用。试一试。

答案 2 :(得分:1)

解释在这里:

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);
}