Converting Descriptors

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Created: vvsnaresh (21 Mar 2007)
Last edited: hamishwillee (20 Jul 2012)

The following is all about how to transform different types of descriptors from one type to another and also covers raw conversions from ASCII to UNICODE and vice versa.

Below are some of the typical cases:

// TBufC -> TBuf
TBufC<16> bufC1(KTestBuf);
TBuf<16> buf1(bufC1.Des());
// TBuf -> TBufC
TBuf<16> buf2(KTestBuf);
TBufC<16> bufC2(buf2);
// TBuf/TBufC -> TPtr
TPtr ptr1(0,0);
TPtr ptr2(bufC2.Des());
// TPtr -> TBuf/TBufC
TBuf<16> buf3 = ptr1;
TBufC<16> bufC3 = ptr2;
// TBuf/TBufC -> TPtrC
TPtrC ptrC1(ptr1);
TPtrC ptrC2(buf2);
TPtrC ptrC3(bufC2);
// All above -> HBufCW
HBufC *pHeap1 = ptrC1.AllocLC();
HBufC *pHeap2 = ptr1.AllocLC();
HBufC *pHeap3 = bufC1.AllocLC();
HBufC *pHeap4 = buf1.AllocLC();
// or assignment operator
HBufC *pHeap5 = HBufC::NewLC(32);
*pHeap5 = ptr1;
// or via Copy method
HBufC *pHeap6 = HBufC::NewMaxLC(32);

The latter scenario helps you in raw conversion from 16-bit to 8-bit data and vice versa:

HBufC8 *pHeap8 = HBufC8::NewMaxLC(buf1.Length());
HBufC *pHeap16 = HBufC::NewMaxLC(pHeap8->Length());

Please notice the very last line in the example above. It is considered to be common mistake to create TDesC& references by calling the Des() method. It is much easier to de-reference the HBufC descriptor to get the desired result.

If you need a more accurate conversion with real UNICODE characters, you can use the CnvUtfConverter class, as in the following simple snippet:

HBufC8 *pHeapUTF = CnvUtfConverter::ConvertFromUnicodeToUtf8L(buf1);
HBufC *pHeapUCS2 = CnvUtfConverter::ConvertToUnicodeFromUtf8L(*pHeapUTF);

Following methods can be used to Convert Unicode text into UTF-8 encoding and vice - versa.

Header : utf.h
Lib : charconv.lib.
// Converts Unicode text into UTF-8 encoding.
CnvUtfConverter::ConvertFromUnicodeToUtf8( DestinationBuffer8, SourceBuffer16 );
//Converts UTF-8 into the Unicode UCS-2 character set.
This page was last modified on 20 July 2012, at 10:05.
51 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.