ivy-jie

progress ...

C++博客 首页 新随笔 联系 聚合 管理
  9 Posts :: 41 Stories :: 6 Comments :: 0 Trackbacks

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.

posted on 2009-07-03 16:40 ivy-jie 阅读(316) 评论(0)  编辑 收藏 引用 所属分类: symbian

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理