Symbian编程总结-基础篇-描述符(2)-TDesC8与TDesC16之间的互转

1、使用Copy

_LIT8(KTestStr, "This is a string");
TBufC8
<50> buf(KTestStr);

TBuf
<100> newBuf;
newBuf.Copy(buf);

TBuf8
<50> newBuf1;
newBuf1.Copy(newBuf);

 

2、使用CCnvCharacterSetConverter

_LIT8(KTestStr, "This is a String"n");
TBufC8<50> buf(KTestStr);

CCnvCharacterSetConverter
* conv = CCnvCharacterSetConverter::NewL();

CleanupStack::PushL(conv);

RFs fs;
User::LeaveIfError(fs.Connect());

if (conv->PrepareToConvertToOrFromL(KCharacterSetIdentifierAscii, fs) != CCnvCharacterSetConverter::EAvailable)
{
User::Leave(KErrNotSupported);
}

HBufC
* str = HBufC::NewL(buf.Length());
CleanupStack::PushL(str);

TPtr ptr 
= str->Des();
TInt state 
= CCnvCharacterSetConverter::KStateDefault;

if (conv->ConvertToUnicode(ptr, buf, state) == CCnvCharacterSetConverter::EErrorIllFormedInput)
{
User::Leave(KErrArgument);
}

fs.Close();
console
->Write(ptr);

CleanupStack::PopAndDestroy(
2); // str, conv
posted @ 2008-09-19 17:52  musée  阅读(1317)  评论(0编辑  收藏  举报