原文出处: http://www.cnblogs.com/felixYeou/archive/2008/09/19/1294364.html
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);
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);