Symbian里面使用ConvertFromUnicodeToUtf8转换中文为什么是乱码?转换英文是正常的。因为要把16位的Unicode汉字保存到文件,所以必须转换成UTF8。
1 void MyUnicode2Utf8::ToUtf8FromUnicode(TDesC16 &aUnicode)
{
if(ibuf8) delete ibuf8;
if(ibuf) delete ibuf;
/ / 如果是中文字符, 这里乘3是正确的, 但是如果是英文就会有问题, 因为它占不满, 所以会有空格出现
int len = aUnicode.Length() * 3 + 1;
ibuf8 = HBufC8::NewL(len);
ibuf = HBufC::NewL(len);
TPtr8 ptrUtf8 = ibuf8->Des();
CnvUtfConverter::ConvertFromUnicodeToUtf8(ptrUtf8, aUnicode);
ibuf->Des().Copy(ptrUtf8);
}
2 The second one takes each character and convert it to the other format. The 16-bit to 8-bit conversion may not always succeed in this case:
Code:
// Get a iBuf8 from a iBuf16 (data are modified)
CnvUtfConverter::ConvertFromUnicodeToUtf8(iBuf8,iBuf16);
// Get a iBuf16 from a iBuf8 (data are modified)
CnvUtfConverter::ConvertToUnicodeFromUtf8(iBuf16,iBuf8);
This second method requires to include the utf.h header and to link against charconv.lib.