1.TDesC16ToHBufC8LC(const TDesC16 &string)
TDesC16ToHBufC8LC(const TDesC16 &string)
{
HBufC8 *buff = HBufC8::NewLC(string.Length());
buff->Des().Copy(string);
return buff;
}
調用時記得pop數據,下同例如
HBufC8* url8 = TDesC16ToHBufC8LC( _L("hello world") );
CleanupStack::PopAndDestroy();
2.TDesC8ToHBufC16LC(const TDesC8 &aString)
TDesC8ToHBufC16LC(const TDesC8 &aString)
{
HBufC16 *buff = HBufC16::NewLC(aString.Length());
buff->Des().Copy(aString);
return buff;
}
3.TDesC16ToTPtrC8(const TDesC16 &aString)
TDesC16ToTPtrC8(const TDesC16 &aString)
{
TPtrC8 ptr8(reinterpret_cast<const TUint8*>(aString.Ptr()),(aString.Length()*2));
return ptr8;
}
4.TDesC8ToTPtrC16(const TDesC8 &aString)
TDesC8ToTPtrC16(const TDesC8 &aString)
{
TPtrC16 ptr16(reinterpret_cast<const TUint16*>(aString.Ptr()),(aString.Length()/2));
return ptr16;
}
5.char* to TPtr8 HBufC
char* aFileName;
TPtr8 tFileName8((unsigned char*)aFileName, strlen(aFileName), strlen(aFileName));
HBufC* tFileName = CnvUtfConverter::ConvertToUnicodeFromUtf8L(tFileName8);
1.TBuf8 to TBuf16
_LIT8(KTDesC8, "hello, 8DescRecipes");
TBuf8<128> buf8(KTDesC8);
TBuf16<128> buf16;
buf16.Copy(buf8);
2.TBuf16 to TPtr
_LIT(KTDesC16, "hello, 16DescRecipes");
TBuf16<128> buf16(KTDesC16);
TPtr8 tp8 = buf16.Collapse();
TPtr tp16 = tp8.Expand();
3.TBuf16 to HBufC8 HBufC16
Location: UTF.H
Link against: charconv.lib
_LIT(KHBufC16, "hello, 16HBufC");
TBuf16<128> buf5(KHBufC16);
HBufC8* pHeapUTF = CnvUtfConverter::ConvertFromUnicodeToUtf8L(buf5);
CleanupStack::PushL(pHeapUTF);
HBufC16* pHeapUCS2 = CnvUtfConverter::ConvertToUnicodeFromUtf8L(*pHeapUTF);
CleanupStack::PushL(pHeapUCS2);
console->Write(*pHeapUCS2);
CleanupStack::PopAndDestroy(2);
4.TDes to C string
_LIT(KFmtCstr, "C-String %s/n");
TBuf<128> buf6(KTDesC16);
TUint cstrBuffer[128];
memcpy(cstrBuffer, buf6.Ptr(), buf6.Size());
5.TDesC to number
_LIT(KNumber, "163");
TInt number = 0;
TLex lex(KNumber);
lex.Val(number);
6.TBuf <===> TBufC
_LIT(KBuf, "hello buf");
TBufC16<16> buf7(KBuf);
TBuf<16> buf9(buf7.Des());
_LIT(KTestBuf, "buf test");
TBuf<16> buf10(KTestBuf);
TBufC<16> buf11(buf10);
7.TBuf TBufC <===> TPtr TPtrC
TPtr ptr(0, 0);
ptr.Set((TUint16*)(buf10.Ptr()), buf10.Length(), 32);;
TPtr ptr2(buf11.Des());
TBuf<16> buf12 = ptr;
TBufC<16> buf13 = ptr;
TPtrC ptrc1(ptr);
TPtrC ptrc2(buf12);
TPtrC ptrc3(buf13);
8. All to HBufC
HBufC* pHeap1 = ptrc1.AllocLC();
HBufC* pHeap2 = HBufC::NewLC(32);
*pHeap2 = ptr;
HBufC* pHeap3 = HBufC::NewMaxLC(32);
pHeap3->Des().Copy(ptr);
CleanupStack::PopAndDestroy(3);