You Are Here:

Community: Wiki

This page was last modified on 8 December 2007, at 13:09.

Converting Descriptors

From Forum Nokia Wiki

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
_LIT(KTestBuf,"Buffer");
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);
ptr1.Set((TUint16*)(buf2.Ptr()),buf2.Length(),32);
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);
pHeap6->Des().Copy(ptr2);

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

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

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.
 
CnvUtfConverter::ConvertToUnicodeFromUtf8(DestinationBuffer16,SourceBuffer);

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia