| Classification: |
Java |
Category: |
JNI |
| Created: |
10/12/99 |
Modified: |
04/28/2005 |
| 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:
{code}
TPtr8 buf;
...
const TText8* ptr = buf.PtrZ();
jstring str = aJNI->NewStringUTF(REINTERPRET_CAST(const char*, ptr));
{code}
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:
{code}
TPtrC8 bufC;
...
TPtr8 buf = bufC.Des()
const TText8* ptr = buf.PtrZ();
jstring str = aJNI->NewStringUTF(REINTERPRET_CAST(const char*, ptr));
{code}
Similarly for an HBufC8 heap descriptor:
{code}
HBufC8* bufC;
...
TPtr8 buf = bufC->Des();
const TText8* ptr = buf->PtrZ();
jstring str = aJNI->NewStringUTF(REINTERPRET_CAST(const char*, ptr));
{code}
Notes:
- 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.
- 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.
- 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 .
|