2011-09-13 4 views
2

Comme vous pouvez le voir, je reçois jbyte * str forme la chaîne utf. Alors chaque caractère de chaîne a deux jbytes sinon un octet?Dans la programmation Android Ndk obtenir la chaîne UTF

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

Répondre

1

ici:

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

vous recevez un tampon de caractères d'un seul octet. Vous pouvez même modifier cela comme ici:

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

parce GetStringUTFChars() est déclaré que le retour const char *.

+0

Non! Le tableau renvoyé par 'GetStringUTFChars' est un caractère Unicode dans un encodage UTF-8 modifié. Chaque personnage prendra de un à six octets. Si vous voulez un jeu de caractères/encodage avec un octet par caractère, utilisez une surcharge 'String.getBytes' ou la classe' Charset'. –