smiler
smiler

Reputation: 315

In Android Ndk programming getting UTF String

As you can see, I get jbyte *str form the utf string. Then each character of string has two jbytes else one byte?

JNIEXPORT jstring JNICALL 

Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt) { 

    char buf[128];
    const jbyte *str;
    str = (env)->GetStringUTFChars(env, prompt, NULL);

    if (str == NULL) { 
        return NULL; / OutOfMemoryError already thrown */ 
    }

    printf("%s", str);

    (*env)->ReleaseStringUTFChars(env, prompt, str); 
    /* We assume here that the user does not type more than * 127 characters */ 
    scanf("%s", buf);

    return (*env)->NewStringUTF(env, buf);
}

Upvotes: 2

Views: 2121

Answers (1)

trashkalmar
trashkalmar

Reputation: 883

Here:

str = (env)->GetStringUTFChars(env, prompt, NULL);

you receive buffer of single byte chars. You may even edit this like here:

const char *str = (env)->GetStringUTFChars(env, prompt, NULL);

because GetStringUTFChars() is declared as returning const char *.

Upvotes: 1

Related Questions