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