windows API实现中文中字符串与GBK、Unicode、UTF-8三种编码互转

  1. 原文出处:https://blog.csdn.net/bladeandmaster88/article/details/54800287
  2. #include <iostream>  
  3. #include <string>  
  4. #include <Windows.h>  
  5. using namespace std;  
  6.   
  7. //gbk转UTF-8  
  8. string GbkToUtf8(const std::string& strGbk)//传入的strGbk是GBK编码  
  9. {  
  10.     //gbk转unicode  
  11.     int len = MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, NULL, 0);  
  12.     wchar_t *strUnicode = new wchar_t[len];  
  13.     wmemset(strUnicode, 0, len);  
  14.     MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, strUnicode, len);  
  15.   
  16.     //unicode转UTF-8  
  17.     len = WideCharToMultiByte(CP_UTF8, 0, strUnicode, -1, NULL, 0, NULL, NULL);  
  18.     char * strUtf8 = new char[len];  
  19.     WideCharToMultiByte(CP_UTF8, 0, strUnicode, -1, strUtf8, len, NULL, NULL);  
  20.   
  21.     std::string strTemp(strUtf8);//此时的strTemp是UTF-8编码  
  22.     delete[]strUnicode;  
  23.     delete[]strUtf8;  
  24.     strUnicode = NULL;  
  25.     strUtf8 = NULL;  
  26.     return strTemp;  
  27. }  
  28.   
  29. //UTF-8转gbk  
  30. string Utf8ToGbk(const std::string& strUtf8)//传入的strUtf8是UTF-8编码  
  31. {  
  32.     //UTF-8转unicode  
  33.     int len = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, NULL, 0);  
  34.     wchar_t * strUnicode = new wchar_t[len];//len = 2  
  35.     wmemset(strUnicode, 0, len);  
  36.     MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, strUnicode, len);  
  37.   
  38.     //unicode转gbk  
  39.     len = WideCharToMultiByte(CP_ACP, 0, strUnicode, -1, NULL, 0, NULL, NULL);  
  40.     char *strGbk = new char[len];//len=3 本来为2,但是char*后面自动加上了\0  
  41.     memset(strGbk, 0, len);  
  42.     WideCharToMultiByte(CP_ACP,0, strUnicode, -1, strGbk, len, NULL, NULL);  
  43.   
  44.     std::string strTemp(strGbk);//此时的strTemp是GBK编码  
  45.     delete[]strUnicode;  
  46.     delete[]strGbk;  
  47.     strUnicode = NULL;  
  48.     strGbk = NULL;  
  49.     return strTemp;  
  50. }  
  51.   
  52. //gbk转unicode (下面的例子没用到)  
  53. wstring GbkToUnicode(const std::string& strGbk)//返回值是wstring  
  54. {  
  55.     int len = MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, NULL, 0);  
  56.     wchar_t *strUnicode = new wchar_t[len];  
  57.     wmemset(strUnicode, 0, len);  
  58.     MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, strUnicode, len);  
  59.   
  60.     std::wstring strTemp(strUnicode);//此时的strTemp是Unicode编码  
  61.     delete[]strUnicode;  
  62.     strUnicode = NULL;  
  63.     return strTemp;  
  64. }  
  65.   
  66. //Unicode转gbk  
  67. string UnicodeToGbk (const std::wstring& strUnicode)//参数是wstring  
  68. {  
  69.     int len = WideCharToMultiByte(CP_ACP, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL);  
  70.     char *strGbk = new char[len];//len=3 本来为2,但是char*后面自动加上了\0  
  71.     memset(strGbk, 0, len);  
  72.     WideCharToMultiByte(CP_ACP,0,strUnicode.c_str(), -1, strGbk, len, NULL, NULL);  
  73.   
  74.     std::string strTemp(strGbk);//此时的strTemp是GBK编码  
  75.     delete[]strGbk;  
  76.     strGbk = NULL;  
  77.     return strTemp;  
  78. }  
  79.   
  80. int main()  
  81. {  
  82.     //1、ANSI/GBK编码  
  83.     string strGbk = "我";  
  84.     int num = strGbk.size();//获取两个字符数,也是我字所占的字节数  
  85.   
  86.     unsigned char* p = (unsigned char*)strGbk.c_str();      
  87.     for (int i = 0; i < num; i++)      
  88.     {      
  89.         printf("%0x", *p);      
  90.         p++;      
  91.     }  //输出ced2 所以我的GBK编码是0xced2  
  92.     printf("\n");     
  93.   
  94.     char gbk[] = {0xce, 0xd2, 0x00}; //加上0x00字符串结束符,不会输出乱码  
  95.     cout<<gbk<<endl;//输出汉字我  
  96.   
  97.   
  98.     //2、unicodde编码  
  99.   
  100.     //方法一  
  101.     //wchar_t str = 0x6211;    
  102.     //wcout.imbue(locale("chs"));   
  103.     //wcout << str << endl;//输出汉字我  
  104.   
  105.     //wchar_t c=L'我';  
  106.     //cout << hex << (short)c << endl<<endl;//输出unicodde编码 6211  
  107.   
  108.     //方法二:  
  109.     wstring strUnicode = L"我";//转成unicode编码  
  110.     num = strUnicode.size()*2;//乘以2,才是我所占的字节数  
  111.     p = (unsigned char*)strUnicode.c_str();      
  112.     for (int i = 0; i < num; i++)      
  113.     {      
  114.         printf("%0x", *p);      
  115.         p++;      
  116.     }  //输出1162 因为默认是小端模式,所以我的unicode编码是0x6211  
  117.     printf("\n");     
  118.   
  119.     wchar_t s[2] = {0x6211, 0x00}; //加上0x00字符串结束符,不会输出乱码  
  120.     wstring str =(wchar_t*)s;  
  121.     cout<<UnicodeToGbk(str)<<endl;//需要先将unicode字符串转成gbk之后才能用cout输出  
  122.   
  123.   
  124.     //3、UTF-8编码  
  125.     string strUtf8  = GbkToUtf8("我");//转成utf8编码  
  126.     num = strUtf8.size();//num=3  
  127.     p = (unsigned char*)strUtf8.c_str();      
  128.     for (int i = 0; i < num; i++)      
  129.     {      
  130.         printf("%0x", *p);      
  131.         p++;      
  132.     }  //输出e68891  
  133.     printf("\n");     
  134.   
  135.     char utf8[] = {0xe6, 0x88, 0x91,0x00}; //加上0x00字符串结束符,不会输出乱码  
  136.     cout<<Utf8ToGbk(utf8)<<endl;//需要先将utf8字符串转成gbk之后才能用cout输出  
  137.   
  138.   
  139.     return 0;  
  140. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值