zoukankan      html  css  js  c++  java
  • C++转换unicode utf-8 gb2312编码

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

    [cpp] view plaincopy
     
    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(" ");  
    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(" ");  
    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
    输出结果:

  • 相关阅读:
    sql_mode=only_full_group_by引起group by查询报错问题
    在Eclipse下使用ant,build项目报jpeg does not exist
    JAVAEE面试之Hibernate缓存
    页面加载中效果实现
    鼠标滑过显示图片
    VSFTP配置参数详解
    FTP上传核心方法
    java对File的特殊操作
    在mybatis下使用日期比对出错
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/lidabo/p/3903623.html
Copyright © 2011-2022 走看看