zoukankan      html  css  js  c++  java
  • VC++互相转码GBK,unicode,utf8

    windows平台下微软的库自带了一些api可用于几种编码格式间的互相转码,其实可以用一个iconv开源跨平台的转码库,那个方法更方便且统一。

    使用前要引入头文件和命名空间

    [cpp] view plain copy
     
     print?
    1. #include <iostream>     
    2. #include <string>     
    3. #include <fstream>     
    4. #include <windows.h>        
    5. using namespace std;   
    [cpp] view plain copy
     
     print?
    1. string GBKToUTF8(std::string& strGBK)   
    2. {    
    3.     string strOutUTF8 = "";    
    4.     WCHAR * str1;    
    5.     int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);    
    6.     str1 = new WCHAR[n];    
    7.     MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);    
    8.     n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);    
    9.     char * str2 = new char[n];    
    10.     WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);    
    11.     strOutUTF8 = str2;    
    12.     delete[]str1;    
    13.     str1 = NULL;    
    14.     delete[]str2;    
    15.     str2 = NULL;    
    16.     return strOutUTF8;  
    17. }    


     

    [cpp] view plain copy
     
     print?
    1. string UTF8ToGBK(const std::string& strUTF8)    
    2. {    
    3.  int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);    
    4.  unsigned short * wszGBK = new unsigned short[len + 1];    
    5.  memset(wszGBK, 0, len * 2 + 2);    
    6.  MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, wszGBK, len);    
    7.   
    8.  len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);    
    9.  char *szGBK = new char[len + 1];    
    10.  memset(szGBK, 0, len + 1);    
    11.  WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL);    
    12.  //strUTF8 = szGBK;     
    13.  std::string strTemp(szGBK);    
    14.  delete[]szGBK;    
    15.  delete[]wszGBK;    
    16.  return strTemp;    
    17. }    


     

    [cpp] view plain copy
     
     print?
    1. void UnicodeToUTF8(wchar_t* strW, char szUtf8[])  
    2.  {  
    3. #ifdef _MSC_VER  
    4.      char* pElementText;  
    5.      int nTextLen;  
    6.   
    7.      nTextLen =  WideCharToMultiByte(  
    8.          CP_UTF8,            // code page  
    9.          0,            // performance and mapping flags  
    10.          strW,    // wide-character string  
    11.          -1,          // number of chars in string.  
    12.          NULL,     // buffer for new string  
    13.          0,          // size of buffer  
    14.          NULL,     // default for unmappable chars  
    15.          NULL  // set when default char used  
    16.          );  
    17.      pElementText = new char[nTextLen+1];  
    18.      memset((void *)pElementText,0,sizeof(char)* (nTextLen+1));  
    19.      WideCharToMultiByte(  
    20.          CP_UTF8,            // code page  
    21.          0,            // performance and mapping flags  
    22.          strW,    // wide-character string  
    23.          -1,          // number of chars in string.  
    24.          pElementText,     // buffer for new string  
    25.          nTextLen,          // size of buffer  
    26.          NULL,     // default for unmappable chars  
    27.          NULL  // set when default char used  
    28.          );  
    29.   
    30.      sprintf_s(szUtf8, nTextLen, "%s", pElementText);  
    31.   
    32.      delete[] pElementText;  
    33.      return ;  
    34. #endif  
    35.  }  


    使用举例:

    [cpp] view plain copy
     
     print?
    1. string test("源编码格式需要转成目标编码格式");    
    2. fstream output("test.txt"); //输出到文件    
    3. output << GBKToUTF8(test);    

    一般来说utf8用的比较多,推荐都转成utf8

    http://blog.csdn.net/u012234115/article/details/42292035

  • 相关阅读:
    脱离Table组件的render苦海
    激活码
    Unittest
    IO多路复用-EPOOL 详细
    PIL-Image、ImageDraw图像标记
    Mysql专场
    xshell命令
    并发编程
    Mysql高级用法
    视图函数中进行sql查询,防止sql注入
  • 原文地址:https://www.cnblogs.com/findumars/p/7252858.html
Copyright © 2011-2022 走看看