Classification: |
Java |
Category: |
JNI |
Created: |
10/12/99 |
Modified: |
07/03/2001 |
Number: |
FAQ-0298 |
Platform: |
Not Applicable |
|
Question: How do I convert an 8-bit descriptor to a Java string?
Answer: To get a jstring (which can be returned to the Java side) from a TPtr8 (or TBuf8) descriptor, you can use the following code:TPtr8 buf; ... const TText8* ptr = buf.PtrZ(); jstring str = aJNI->NewStringUTF(REINTERPRET_CAST(const char*, ptr));
If you have a TPtrC or a TBufC descriptor, you will not be able to access the PtrZ() method directly, but you can do this through an intermediate TPtr8 as follows:
TPtrC8 bufC; ... TPtr8 buf = bufC.Des() const TText8* ptr = buf.PtrZ(); jstring str = aJNI->NewStringUTF(REINTERPRET_CAST(const char*, ptr));
Similarly for an HBufC8 heap descriptor:
HBufC8* bufC; ... TPtr8 buf = bufC->Des(); const TText8* ptr = buf->PtrZ(); jstring str = aJNI->NewStringUTF(REINTERPRET_CAST(const char*, ptr));
Notes:
1. The PtrZ() serves to append a null terminator to the string to which it is applied and return a const pointer to unsigned data. You must ensure that there is enough space in the buffer for the null to be appended, other a User
23 Panic will occur.
2. The REINTERPRET_CAST is necessary because ptr is a pointer to unsigned data whereas the constructor for a Java string requires a pointer to (null-terminated) signed data.
3. If the EPOC descriptor contains cp1252 special characters, the conversion should be done via an intermediate 16-bit buffer
descriptor. The details of how to do this under ER5 are given in the following KB entry dealing with conversion in the opposite
direction. The further conversion from a 16-bit descriptor to a Java string is described here .
|