Jstring (JNI) - std :: string (C ++) with utf8 characters

How to convert jstring(JNI) to std::string(C ++) with characters utf8?

this is my code. it worked with non-utf8 characters, but this is not the case 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;
}
+11
source share
3 answers

After a lot of time to find a solution. I found a way:

Java utf16 Unicode 2 (utf16). jstring utf16. std::string c++ , , , jstring JNI c++, utf16 .

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, utf8.

GetStringChars jbyte *, jbytes c++

( 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;
}
+15
jboolean isCopy;
const char *convertedValue = (env)->GetStringUTFChars(yourJStringParam, &isCopy);
std::string string = std::string(convertedValue, length)

. .

+2

:

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

Source: https://habr.com/ru/post/1667602/


All Articles