This page was last modified 13:09, 8 December 2007.
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 Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to read into TBuf16<56> (symbian yucca) | capricorn | General Symbian C++ | 1 | 2007-03-10 09:30 |
| Showing gif's in Series 60 with C++ | teemut80 | General Symbian C++ | 0 | 2003-11-04 09:53 |
| reading utf-8 files | Christophe.A | General Symbian C++ | 6 | 2008-07-11 07:49 |
| AudioStreaming for playing AMR | khurshed79 | Symbian Media (Graphics & Sounds) | 1 | 2005-08-21 17:19 |
| Image Decoding Issue | mailkamlesh | Symbian Media (Graphics & Sounds) | 3 | 2007-05-21 04:40 |
