zoukankan      html  css  js  c++  java
  • C++ 编码转换

    CEncode.h文件
     1 #ifndef _CENCODE_H_
     2 #define _CENCODE_H_
     3 
     4 #include <iostream>
     5 #include <string>
     6 #include <windows.h>
     7 using namespace std;
     8 
     9 class CEncode
    10 {
    11 public:
    12     //Converting a WChar string to a Ansi string
    13     static    std::string WChar2Ansi(LPCWSTR pwszSrc);
    14 
    15     //Converting a Ansi string to WChar string
    16     static std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen);
    17 
    18     //Unicode to ANSI
    19     static std::string UnicodeToANSI(const wstring& wstr);
    20 
    21     // Unicode to UTF-8
    22     static std::string UnicodeToUTF8( const wstring& wstr );
    23 
    24     // ANSI to Unicode
    25     static std::wstring ANSIToUnicode( const string& str );
    26 
    27     //UTF-8 to Unicode
    28     static std::wstring UTF8ToUnicode(const string& inUtf8);
    29 
    30     //GBK to UTF-8
    31     static std::string GBKToUTF8(const std::string& strGBK);
    32 
    33     //UTF-8 to GBK
    34     static std::string UTF8ToGBK(const std::string& strUTF8);
    35 
    36     //UTF-8 to GB2312
    37     static std::string UTF8ToGB2312(const char* utf8);
    38 
    39     //GB2312 to UTF-8
    40     static std::string GB2312ToUTF8(const char* gb2312);
    41 
    42 };
    43 #endif
    View Code
    CEncode.cpp文件
      1 #include "CEncode.h"
      2 
      3 std::string CEncode::WChar2Ansi(LPCWSTR pwszSrc)
      4 {
      5     int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
      6 
      7     if (nLen<= 0) return std::string("");
      8 
      9     char* pszDst = new char[nLen];
     10 
     11     if (NULL == pszDst) return std::string("");
     12 
     13     WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
     14 
     15     pszDst[nLen -1] = 0;
     16 
     17     std::string strTemp(pszDst);
     18 
     19     delete [] pszDst;
     20 
     21     return strTemp;
     22 }
     23 
     24 std::wstring CEncode::Ansi2WChar(LPCSTR pszSrc, int nLen)
     25 {
     26     int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);
     27 
     28     if(nSize <= 0) return NULL;
     29 
     30     WCHAR *pwszDst = new WCHAR[nSize+1];
     31 
     32     if( NULL == pwszDst) return NULL;
     33 
     34     MultiByteToWideChar(CP_ACP, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);
     35 
     36     pwszDst[nSize] = 0;
     37 
     38     if( pwszDst[0] == 0xFEFF) // skip Oxfeff
     39         for(int i = 0; i < nSize; i ++)
     40             pwszDst[i] = pwszDst[i+1];
     41 
     42     wstring wcharString(pwszDst);
     43 
     44     delete pwszDst;
     45 
     46     return wcharString;
     47 }
     48 
     49 std::string CEncode::UnicodeToANSI(const wstring& wstr)
     50 {
     51     int Length;
     52     Length = ::WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,NULL,0,NULL,0);
     53 
     54     char *pChar= new char[Length+1];
     55     memset(pChar,0,Length+1);
     56 
     57     ::WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,pChar,Length,NULL,0);
     58 
     59     string str = pChar;
     60     delete [] pChar;
     61     return str;
     62 }
     63 
     64 
     65 std::string CEncode::UnicodeToUTF8( const wstring& wstr )
     66 {
     67     int    Length;
     68     // wide char to multi char
     69     Length = ::WideCharToMultiByte( CP_UTF8,0,wstr.c_str(),-1,NULL,0,NULL,NULL );
     70 
     71     char* pElementText = new char[Length + 1];
     72     memset( ( void* )pElementText, 0, sizeof( char ) * ( Length + 1 ) );
     73     ::WideCharToMultiByte( CP_UTF8,0,wstr.c_str(),-1,pElementText,Length,NULL,NULL );
     74 
     75     string strText;
     76     strText = pElementText;
     77     delete[] pElementText;
     78     return strText;
     79 }
     80 
     81 std::wstring CEncode::ANSIToUnicode( const string& str )
     82 {
     83     int  unicodeLen = ::MultiByteToWideChar( CP_ACP,0,str.c_str(),-1,NULL,0 ); 
     84 
     85     wchar_t *  pUnicode; 
     86     pUnicode = new  wchar_t[unicodeLen+1]; 
     87     memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
     88 
     89     ::MultiByteToWideChar( CP_ACP,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen ); 
     90 
     91     wstring  wstr; 
     92     wstr = ( wchar_t* )pUnicode;
     93     delete[]  pUnicode;
     94 
     95     return  wstr;
     96 }
     97 
     98 wstring CEncode::UTF8ToUnicode(const string& inUtf8)
     99 {
    100     int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,0,inUtf8.c_str(),-1,NULL,0 ); 
    101 
    102     wchar_t *  pUnicode; 
    103     pUnicode = new  wchar_t[unicodeLen+1]; 
    104     memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
    105 
    106     ::MultiByteToWideChar( CP_UTF8,0,inUtf8.c_str(),-1,(LPWSTR)pUnicode,unicodeLen ); 
    107 
    108     wstring  wstr; 
    109     wstr = ( wchar_t* )pUnicode;
    110     delete[]  pUnicode;
    111 
    112     return  wstr;
    113 }
    114 
    115 
    116 std::string CEncode::GBKToUTF8(const std::string& strGBK) 
    117 { 
    118     string strOutUTF8 = ""; 
    119     WCHAR * str1;
    120     int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0); 
    121     str1 = new WCHAR[n]; 
    122     MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n); 
    123 
    124     n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
    125     char * str2 = new char[n]; 
    126     WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL); 
    127     strOutUTF8 = str2; 
    128     delete[]str1; 
    129     str1 = NULL; 
    130     delete[]str2;
    131     str2 = NULL; 
    132     return strOutUTF8;
    133 }
    134 
    135 
    136 
    137 std::string CEncode::UTF8ToGB2312(const char* utf8)
    138 {
    139     int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    140 
    141     wchar_t* wstr = new wchar_t[len+1];
    142 
    143     memset(wstr, 0, len+1);
    144 
    145     MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    146 
    147     len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    148 
    149     char* str = new char[len+1];
    150 
    151     memset(str, 0, len+1);
    152 
    153     WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    154     if(wstr) delete[] wstr;
    155 
    156     string temp = str;
    157     return temp;
    158 }
    159 
    160 
    161 std::string CEncode::GB2312ToUTF8(const char* gb2312)
    162 {
    163     int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
    164 
    165     wchar_t* wstr = new wchar_t[len+1];
    166 
    167     memset(wstr, 0, len+1);
    168 
    169     MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
    170 
    171     len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
    172 
    173     char* str = new char[len+1];
    174 
    175     memset(str, 0, len+1);
    176 
    177     WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
    178 
    179     if(wstr) delete[] wstr;
    180 
    181     string temp = str;
    182     return temp;
    183 }
    View Code

     补充:http://blog.csdn.net/sjy88813/article/details/6662879

  • 相关阅读:
    题解-亚瑟王的宫殿
    学习总结-网络流
    题解-牛奶模式
    题解-最长回文子串
    最大公约数
    DetachedCriteria类中uniqueResult()方法与list()方法区别
    MD5加密方法
    dom4j读取xml文档,通过JDBC写入数据库
    Numpy增加一列,指定概率指定参数
    python 贪吃蛇
  • 原文地址:https://www.cnblogs.com/chechen/p/4081212.html
Copyright © 2011-2022 走看看