| Classification: |
Java |
Category: |
JNI |
| Created: |
09/23/99 |
Modified: |
04/27/2005 |
| 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:
{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;
}
{code}
To get a jstring (which can be returned to the Java side) from a TPtrC16 (or TBufC16) descriptor, you can use the following code:
{code}
TPtrC16 buf;
...
jstring str = aJNI->NewString(buf.Ptr(), buf.Length());
{code}
Similarly for an HBufC16 heap descriptor:
{code}
HBufC16* buf;
...
jstring str = aJNI->NewString(buf->Ptr(), buf->Length());
{code}
|