Symbian
Symbian OS Library

FAQ-0277 How do I convert between Java strings and Unicode descriptors?

[Index][spacer] [Previous] [Next]



 

Classification: Java Category: JNI
Created: 09/23/99 Modified: 07/03/2001
Number: FAQ-0277
Platform: Not Applicable

Question:
How do I convert between Java strings and Unicode descriptors?

Answer:
To get a TPtrC from a Java string (received in a JNI context as a jstring), you can use the following code:
JNIEXPORT jint JNICALL Java_Example__1native(JNIEnv* aJNI, jclass, jstring aString)
    {
    const jchar* const ptr = aJNI->GetStringChars(aString, NULL);
    const jsize len = aJNI->GetStringLength(aString);
    TPtrC16 ptr16(ptr, len);
    jint error = NativeStuff(ptr16); // must not modify data!
    aJNI->ReleaseStringChars(aString, ptr); // original data no longer needed

    return error;
    }
    To get a jstring (which can be returned to the Java side) from a TPtrC16 (or TBufC16) descriptor, you can use the following code:

      TPtrC16 buf;
      ...
      jstring str = aJNI->NewString(buf.Ptr(), buf.Length());

      Similarly for an HBufC16 heap descriptor:

        HBufC16* buf;
        ...
        jstring str = aJNI->NewString(buf->Ptr(), buf->Length())
         
        ;