Symbian
Symbian OS Library

FAQ-0816 How do I convert an 8-bit byte array to a TDesC16?

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



 

Classification: C++ Category: Base
Created: 09/09/2002 Modified: 09/11/2002
Number: FAQ-0816
Platform: Symbian OS v6.0, Symbian OS v6.1, Symbian OS v7.0

Question:
I want to send text stored as a TText8* array to the DrawText() method of a bitmapped graphics context. But I need to convert it first to a TDesC16. What's the recommended way to do this?

Answer:
A TPtr8 constructor will do a conversion from TText8* and the Copy() method of TDesC16 will do the conversion to 16-bit, as follows:
    TText8[] myData=...;
    const TInt length = sizeof(myData); // length of the array
    TPtr8 ptr(myData, length, length); 
    TBuf16 buf16;
    buf16.Copy(ptr);
    There is incidentally a corresponding Copy() method of TDesC8 which will do the reverse conversion from 16-bit to 8-bit, provided the data values do not exceed 255 (decimal).