zoukankan      html  css  js  c++  java
  • C++ GB2312 和 utf8 在win32下 互转

     1 string ANSItoUTF8(const char* strAnsi)
     2 {
     3     //获取转换为宽字节后需要的缓冲区大小,创建宽字节缓冲区,936为简体中文GB2312代码页  
     4     int nLen = MultiByteToWideChar(CP_ACP, NULL, strAnsi, -1, NULL, NULL);
     5     WCHAR *wszBuffer = new WCHAR[nLen + 1];
     6     nLen = MultiByteToWideChar(CP_ACP, NULL, strAnsi, -1, wszBuffer, nLen);
     7     wszBuffer[nLen] = 0;
     8     //获取转为UTF8多字节后需要的缓冲区大小,创建多字节缓冲区  
     9     nLen = WideCharToMultiByte(CP_UTF8, NULL, wszBuffer, -1, NULL, NULL, NULL, NULL);
    10     CHAR *szBuffer = new CHAR[nLen + 1];
    11     nLen = WideCharToMultiByte(CP_UTF8, NULL, wszBuffer, -1, szBuffer, nLen, NULL, NULL);
    12     szBuffer[nLen] = 0;
    13 
    14     string s1 = szBuffer;
    15     //内存清理  
    16     delete[]wszBuffer;
    17     delete[]szBuffer;
    18     return s1;
    19 }
    20 
    21 string UTF8toANSI(const char* strUTF8)
    22 {
    23     //获取转换为多字节后需要的缓冲区大小,创建多字节缓冲区  
    24     int nLen = MultiByteToWideChar(CP_UTF8, NULL, strUTF8, -1, NULL, NULL);
    25     WCHAR *wszBuffer = new WCHAR[nLen + 1];
    26     nLen = MultiByteToWideChar(CP_UTF8, NULL, strUTF8, -1, wszBuffer, nLen);
    27     wszBuffer[nLen] = 0;
    28 
    29     nLen = WideCharToMultiByte(936, NULL, wszBuffer, -1, NULL, NULL, NULL, NULL);
    30     CHAR *szBuffer = new CHAR[nLen + 1];
    31     nLen = WideCharToMultiByte(936, NULL, wszBuffer, -1, szBuffer, nLen, NULL, NULL);
    32     szBuffer[nLen] = 0;
    33 
    34     string s1 = szBuffer;
    35     //清理内存  
    36     delete[]szBuffer;
    37     delete[]wszBuffer;
    38     return s1;
    39 }

    转载

  • 相关阅读:
    python--输出spwm的数组
    爬虫二:爬取糗事百科段子
    爬虫一:爬取信息
    Python中的切片操作
    JSON
    python 中的异常处理与种类
    register的功能
    static的功能
    网络安全的认识
    VMware5.5-vCenter Converter(转换)
  • 原文地址:https://www.cnblogs.com/gardenofhu/p/8423099.html
Copyright © 2011-2022 走看看