Symbian字串轉換

來源:互聯網
上載者:User

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);

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.