ANSI UTF16 UTF8转换

http://blog.csdn.net/zaccheodong/article/details/6951868


NSI UTF16 UTF8转换

分类: C++ 系统   1807人阅读  评论(0)  收藏  举报
[cpp]  view plain copy
  1. // ansi在本机是MBCS(实际上是DBCX) 转为UNIcode默认的UTF16编码  
  2. std::wstring ANSI_2_UTF16( const string& strANSI )  
  3. {  
  4.     int nUnicodeLength = ::MultiByteToWideChar(CP_ACP,0,strANSI.c_str(),-1,NULL,0)  ;  
  5.   
  6.     wstring strUTF16(nUnicodeLength,_T(' '));  
  7.     int nRet = ::MultiByteToWideChar(CP_ACP,0,strANSI.c_str(),-1,&strUTF16[0],nUnicodeLength);  
  8.     ASSERT(0 != nRet);  
  9.       
  10.     return strUTF16;  
  11. }  
  12. // UNIcode默认的UTF16编码转为 ansi 在本机是MBCS(实际上是DBCX)   
  13. std::string UTF16_2_ANSI( const wstring& strUTF16 )  
  14. {  
  15.     int nANSILength = ::WideCharToMultiByte(CP_ACP,0,strUTF16.c_str(),-1,NULL,0,0,0);  
  16.   
  17.     string strANSI(nANSILength,' ');  
  18.     int nRet = ::WideCharToMultiByte(CP_ACP,0,strUTF16.c_str(),-1,&strANSI[0],nANSILength,0,0);  
  19.     ASSERT(0 != nRet);  
  20.     return strANSI;  
  21. }  
  22. // UNIcode默认的UTF16编码转为 UTF8编码  
  23. std::string UTF16_2_UTF8( const wstring& strUTF16 )  
  24. {  
  25.     int nUTF8Length = ::WideCharToMultiByte(CP_UTF8,  
  26.         0,  
  27.         strUTF16.c_str(),  
  28.         -1,  
  29.         NULL,  
  30.         0,  
  31.         0,0);  
  32.       
  33.     string strUTF8(nUTF8Length+1,'\0');  
  34.     int nRet = ::WideCharToMultiByte(CP_UTF8,  
  35.         0,  
  36.         strUTF16.c_str(),  
  37.         -1,  
  38.         &strUTF8[0],  
  39.         nUTF8Length+1,  
  40.         0,  
  41.         0);  
  42.   
  43.   
  44.     return strUTF8;  
  45. }  
  46. // UNIcode UTF8编码 转为 UNIcode默认的UTF16编码  
  47. std::wstring UTF8_2_UTF16( const string& strUTF8 )  
  48. {  
  49.     int nUTF16Length = ::MultiByteToWideChar(CP_UTF8,0,strUTF8.c_str(),-1,NULL,0);  
  50.   
  51.     nUTF16Length += 1;  
  52.     wstring strUTF16(nUTF16Length ,' ');  
  53.   
  54.     int nRet = ::MultiByteToWideChar(CP_UTF8,0,strUTF8.c_str(),-1,  
  55.         &strUTF16[0],nUTF16Length);  
  56.     ASSERT(0 != nRet);  
  57.   
  58.     return strUTF16;  
  59. }  
  60.   
  61. //UTF8  编码的UNICODE字符集转为ANSI编码,先转换为UTF16编码,再转换为ANSI编码  
  62. std::string UTF8_2_ANSI( const string& strUTF8 )  
  63. {  
  64.     wstring wstrUTF16 = UTF8_2_UTF16(strUTF8);  
  65.     string  strANSI   = UTF16_2_ANSI(wstrUTF16);  
  66.   
  67.     return strANSI;  
  68. }  
  69. //ANSI编码 转为 UTF8编码的,先转换为UTF16编码,再转换为UTF8编码  
  70. std::string ANSI_2_UTF8( const string& strANSI )  
  71. {  
  72.     wstring wstrUTF16 = ANSI_2_UTF16(strANSI);  
  73.     string strUTF8    = UTF16_2_UTF8(wstrUTF16);  
  74.   
  75.     return strUTF8;  
  76. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值