C++转换unicode utf-8 gb2312编码

简介: windows开发环境下用VC++6.0 对unicode 、utf-8、 gb2312 三种编码格式之间的转换方法:   [cpp] view plaincopy   #include    #include    #include    using namesp...

windows开发环境下用VC++6.0 对unicode 、utf-8、 gb2312 三种编码格式之间的转换方法:

 

[cpp]  view plain copy
 
  1. #include <iostream>  
  2. #include <string>  
  3. #include <Windows.h>  
  4. using namespace std;  
  5.   
  6. void unicodeToUTF8(const wstring &src, string& result)  
  7. {  
  8.     int n = WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, 0, 0, 0, 0 );  
  9.     result.resize(n);  
  10.     ::WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, (char*)result.c_str(), result.length(), 0, 0 );  
  11. }  
  12.   
  13. void unicodeToGB2312(const wstring& wstr , string& result)  
  14. {  
  15.     int n = WideCharToMultiByte( CP_ACP, 0, wstr.c_str(), -1, 0, 0, 0, 0 );  
  16.     result.resize(n);  
  17.     ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, (char*)result.c_str(), n, 0, 0 );  
  18. }  
  19.   
  20. void utf8ToUnicode(const string& src, wstring& result)  
  21. {  
  22.     int n = MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, NULL, 0 );  
  23.     result.resize(n);  
  24.     ::MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());  
  25. }  
  26.   
  27. void gb2312ToUnicode(const string& src, wstring& result)  
  28. {  
  29.     int n = MultiByteToWideChar( CP_ACP, 0, src.c_str(), -1, NULL, 0 );  
  30.     result.resize(n);  
  31.     ::MultiByteToWideChar( CP_ACP, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());  
  32. }  
  33.   
  34. void printByte(string str)  
  35. {  
  36.     int i=0;  
  37.     for (i=0; i<str.length(); i++)  
  38.     {  
  39.         printf("%X ",(unsigned char)str.at(i));  
  40.     }  
  41.     printf("\n");  
  42. }  
  43.   
  44. void wprintByte(wstring str)  
  45. {  
  46.     int i=0;  
  47.     for (i=0; i<str.length()*sizeof(wchar_t); i++)  
  48.     {  
  49.         printf("%X ",*((unsigned char*)str.c_str()+i));  
  50.     }  
  51.     printf("\n");  
  52. }  
  53.   
  54. int main()  
  55. {  
  56.     string strText = "AB汉字";  
  57.     string strUTF8;  
  58.     wstring wstrUnicode;  
  59.     string strGB2312;  
  60.   
  61.     gb2312ToUnicode(strText, wstrUnicode);  
  62.     printf("Unicode=");  
  63.     wprintByte(wstrUnicode);  
  64.   
  65.     unicodeToUTF8(wstrUnicode, strUTF8);  
  66.     printf("UTF-8  =");  
  67.     printByte(strUTF8);  
  68.       
  69.     utf8ToUnicode(strUTF8,wstrUnicode);  
  70.     printf("Unicode=");  
  71.     wprintByte(wstrUnicode);  
  72.       
  73.     unicodeToGB2312(wstrUnicode,strGB2312);  
  74.     printf("GB2312 =");  
  75.     printByte(strGB2312);  
  76.   
  77.     return 0;        
  78. }  

这里用“AB汉字”这样一个字符串做测试,它的ASCII编码为41 42 BA BA D7 D6
输出结果:

 

目录
相关文章
|
1月前
|
存储 自然语言处理 Linux
探究C/C++编码世界:从字符编码到中文处理之艺(三)
探究C/C++编码世界:从字符编码到中文处理之艺
44 2
|
1月前
|
自然语言处理 C++
探究C/C++编码世界:从字符编码到中文处理之艺(二)
探究C/C++编码世界:从字符编码到中文处理之艺
37 2
|
1月前
|
存储 自然语言处理 程序员
探究C/C++编码世界:从字符编码到中文处理之艺(一)
探究C/C++编码世界:从字符编码到中文处理之艺
34 1
|
2月前
|
存储 算法 搜索推荐
【编码狂想】探索C++ STL:提升编程效率的强大工具集
【编码狂想】探索C++ STL:提升编程效率的强大工具集
31 0
|
8月前
|
编解码 Linux 编译器
使用 C++ 方式实现 GBK 到 UTF-8 转码 (win / linux)
使用 C++ 的方式处理在 Windows 平台和 Linux 平台,编码字符集从 GBK 到 UTF-8 转码,C++ 存在多种方式实现
925 1
C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)
C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)
C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)
|
2月前
|
机器学习/深度学习 算法 C语言
【编码狂想】深度探索C++编程之旅:“数组、字符串、函数与KMP算法解密“
【编码狂想】深度探索C++编程之旅:“数组、字符串、函数与KMP算法解密“
73 0
|
29天前
|
安全 编译器 C语言
MISRA C++ 、Google C++ 、AUTOSAR Adaptive Platform编码 C++ 规范总结
MISRA C++ 、Google C++ 、AUTOSAR Adaptive Platform编码 C++ 规范总结
88 1
|
9月前
|
编解码 Linux 开发工具
C++实现RTMP协议发送H.264编码及AAC编码的音视频,摄像头直播
C++实现RTMP协议发送H.264编码及AAC编码的音视频,摄像头直播
173 0
|
9月前
|
C++
C++实现对RGB图片进行编码
介绍了如何利用得到的RGB信息重新对RGB图片进行编码,以及对其他图片如BMP所得到的RGB信息进行编码从而得到*.jpg文件。
114 0