Windows 下字节转换

Windows 下字节转换

  1 #include <string>
  2 #include <windows.h>
  3 
  4 class CharsConversion
  5 {
  6 public:
  7     static bool MultiByte2UTF8( const std::string&, std::string& );  
  8 
  9     static bool UTF82MultiByte( const std::string&, std::string& );
 10 
 11     static bool MultiByte2Unicode( const std::string&, std::wstring&  );
 12 
 13     static bool Unicode2MultiByte( const std::wstring&,  std::string& );
 14 
 15     static bool UTF82Unicode( const std::string&, std::wstring& );
 16 
 17     static bool Unicode2UTF8( const std::wstring&, std::string& );
 18 };
 19 
 20 bool CharsConversion::MultiByte2UTF8( const std::string &strMB, std::string& strUTF8 )  
 21 {  
 22     //Note: If this cbMultiByte is –1, the string is assumed to be null terminated and the length is calculated automatically.  
 23     //Note: If cbMultiByte is strMB.size(), this function will not include the null terminated, it's an error.
 24     //Note: Or, if cbMultiByte is strMB.size() + 1, this function will be right.
 25     int nWCLen = MultiByteToWideChar( CP_ACP, 0, strMB.c_str(), -1, NULL, 0 );  
 26 
 27     wchar_t* pszWC = new (std::nothrow) wchar_t[nWCLen];  
 28     if ( nullptr == pszWC ) return false;
 29 
 30     //Note: The return value includes the NULL termination character.  
 31     int nRtn = MultiByteToWideChar( CP_ACP, 0, strMB.c_str(), -1, pszWC, nWCLen );  
 32     if( nRtn != nWCLen ) { delete[] pszWC; return false; }  
 33 
 34     // WindChar 2 UTF8  
 35     //Note: nWClen and -1 both ok.
 36     int nUTF8Len = WideCharToMultiByte( CP_UTF8, 0, pszWC, nWCLen, NULL, 0, NULL, NULL );  
 37     if ( nUTF8Len <= 0 ) { delete [] pszWC; return false; }  
 38 
 39     strUTF8.resize( nUTF8Len );  
 40     nRtn = WideCharToMultiByte( CP_UTF8, 0, pszWC, nWCLen, &strUTF8[0], nUTF8Len, NULL, NULL );      
 41     delete [] pszWC;  
 42 
 43     if ( nRtn != nUTF8Len ) { strUTF8.clear(); return false; }  
 44 
 45     return true;  
 46 } 
 47 
 48 bool CharsConversion::UTF82MultiByte( const std::string &strUTF8, std::string &strMB )  
 49 {  
 50     //Note: cchWideChar must be -1 or strUTF8.size()+1.
 51     int nWCLen = MultiByteToWideChar( CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0 );  
 52 
 53     WCHAR* pszWC = new (std::nothrow) WCHAR[nWCLen];  
 54     if ( nullptr == pszWC ) return false;
 55 
 56     int nRtn = MultiByteToWideChar( CP_UTF8, 0, strUTF8.c_str(), -1, pszWC, nWCLen );  
 57     if ( nRtn != nWCLen ) { delete[] pszWC; return false; }  
 58 
 59     //WideChar 2 MB   
 60     int nMBLen = WideCharToMultiByte( CP_ACP, 0, pszWC, nWCLen/*or -1*/, NULL, 0, NULL, NULL );  
 61     if ( nMBLen <= 0 ) { delete [] pszWC; return false; } 
 62 
 63     strMB.resize( nMBLen );  
 64     nRtn = WideCharToMultiByte( CP_ACP, 0, pszWC, nWCLen/*or -1*/, &strMB[0], nMBLen, NULL, NULL );  
 65     delete [] pszWC;  
 66 
 67     if ( nRtn != nMBLen ) { strMB.clear(); return false; } 
 68 
 69     return true;  
 70 }  
 71 
 72 bool CharsConversion::MultiByte2Unicode( const std::string &strMB, std::wstring &strUC )  
 73 {  
 74     //Note: cbMultiByte can be strMB.size() + 1 or -1.
 75     int nUCLen = MultiByteToWideChar( CP_ACP, 0, strMB.c_str(), -1, NULL, 0 );  
 76 
 77     if ( nUCLen <= 0 )  return false;  
 78 
 79     strUC.resize( nUCLen );  
 80 
 81     int nRtn = MultiByteToWideChar( CP_ACP, 0, strMB.c_str(), -1, &strUC[0], nUCLen );  
 82 
 83     if ( nRtn != nUCLen ) { strUC.clear(); return false; }  
 84 
 85     return true;  
 86 }  
 87 
 88 bool CharsConversion::Unicode2MultiByte( const std::wstring &strUC,  std::string &strMB )  
 89 {  
 90     //Note: cchWideChar  must be -1 or strUC.size()+1.   
 91     int nMBLen = WideCharToMultiByte( CP_ACP, 0, strUC.c_str(), -1, NULL, 0, NULL, NULL );  
 92 
 93     if ( nMBLen <= 0 ) return false;  
 94 
 95     strMB.resize( nMBLen ); 
 96 
 97     int nRtn = WideCharToMultiByte( CP_ACP, 0, strUC.c_str(), -1, &strMB[0], nMBLen, NULL, NULL );  
 98 
 99     if( nRtn != nMBLen ) { strMB.clear(); return false; }  
100 
101     return true;  
102 }  
103 
104 bool CharsConversion::UTF82Unicode( const std::string &strUTF8, std::wstring &strUC )  
105 { 
106     //Note: cbMultiByte can be strUTF8.size() + 1 or -1.
107     int nUCLen = MultiByteToWideChar( CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0 );  
108 
109     if ( nUCLen <= 0 ) return false;
110 
111     strUC.resize( nUCLen );  
112 
113     int nRtn = MultiByteToWideChar( CP_UTF8, 0, strUTF8.c_str(), -1, &strUC[0], nUCLen );  
114 
115     if ( nRtn != nUCLen ) { strUC.clear(); return false; }  
116 
117     return true;  
118 }  
119 
120 bool CharsConversion::Unicode2UTF8( const std::wstring &strUC, std::string &strUTF8 )  
121 {   
122     //Note: cchWideChar  must be -1 or strUC.size()+1. 
123     int nUTF8Len = WideCharToMultiByte( CP_UTF8, 0, strUC.c_str(), -1, NULL, 0, NULL, NULL );
124 
125     if ( nUTF8Len <= 0 ) return false;  
126 
127     strUTF8.resize( nUTF8Len ); 
128 
129     int nRtn = WideCharToMultiByte( CP_UTF8, 0, strUC.c_str(), -1, &strUTF8[0], nUTF8Len, NULL, NULL );  
130 
131     if ( nRtn != nUTF8Len ) { strUTF8.clear(); return false; }  
132 
133     return true;  
134 } 
原文地址:https://www.cnblogs.com/wanghaiyang1930/p/5264806.html