c++中char*wchar_t*stringwstring之间的相互转换

  1 string U2A(const wstring& str)//Unicode字符转Ascii字符
  2 {
  3     string strDes;
  4     if ( str.empty() )
  5         goto __end;
  6     int nLen=::WideCharToMultiByte(CP_ACP, 0, str.c_str(), str.size(), NULL, 0, NULL, NULL);
  7     if ( 0==nLen )
  8         goto __end;
  9     char* pBuffer=new char[nLen+1];
 10     memset(pBuffer, 0, nLen+1);
 11     ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), str.size(), pBuffer, nLen, NULL, NULL);
 12     pBuffer[nLen]='';
 13     strDes.append(pBuffer);
 14     delete[] pBuffer;
 15 __end:
 16     return strDes;
 17 }
 18 
 19 wstring A2U(const string& str)//Ascii字符转
 20 {
 21     wstring strDes;
 22     if ( str.empty() )
 23         goto __end;
 24     int nLen=::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
 25     if ( 0==nLen )
 26         goto __end;
 27     wchar_t* pBuffer=new wchar_t[nLen+1];
 28     memset(pBuffer, 0, nLen+1);
 29     ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), pBuffer, nLen);
 30     pBuffer[nLen]='';
 31     strDes.append(pBuffer);
 32     delete[] pBuffer;
 33 __end:
 34     return strDes;
 35 }
 36 
 37 string U2Utf(const wstring& wstrUnicode)//Unicode转utf8  
 38 {  
 39     string strRet;
 40     if( wstrUnicode.empty() )
 41         return strRet;
 42     int nLen = WideCharToMultiByte(CP_UTF8, 0, wstrUnicode.c_str(), -1, NULL, 0, NULL, NULL);  
 43     char* pBuffer=new char[nLen+1];
 44     pBuffer[nLen] = '';
 45     nLen = WideCharToMultiByte(CP_UTF8, 0, wstrUnicode.c_str(), -1, pBuffer, nLen, NULL, NULL); 
 46     strRet.append(pBuffer);
 47     delete[] pBuffer;
 48     return strRet;  
 49 }
 50 
 51 wstring Utf2U(const string &str)//utf8转Unicode
 52 {
 53     int u16Len = ::MultiByteToWideChar(CP_UTF8, NULL,str.c_str(),(int)str.size(), NULL, 0);
 54     wchar_t* wstrBuf = new wchar_t[u16Len + 1];
 55     ::MultiByteToWideChar(CP_UTF8, NULL, str.c_str(),(int)str.size(), wstrBuf, u16Len);
 56     wstrBuf[u16Len] = L'';
 57     wstring wStr;
 58     wStr.assign(wstrBuf, u16Len);
 59     delete [] wstrBuf;
 60     return wStr;
 61 }
 62 //分割字符串
 63 bool SplitString(const wstring& strSource,const wstring& strFlag, vector<wstring>& paramList)
 64 {
 65     if ( strSource.empty() || strFlag.empty() )
 66         return false;
 67     paramList.clear();
 68     size_t nBeg = 0;
 69     size_t nFind = strSource.find(strFlag, nBeg);
 70     if ( nFind == std::wstring::npos )
 71         paramList.push_back(strSource);
 72     else
 73     {
 74         while ( true )
 75         {
 76             if ( nFind != nBeg )
 77                 paramList.push_back(strSource.substr(nBeg, nFind-nBeg));
 78             nBeg = nFind + strFlag.size();
 79             if ( nBeg == strSource.size() )
 80                 break;
 81             nFind = strSource.find(strFlag, nBeg);
 82             if ( nFind == std::wstring::npos )
 83             {
 84                 paramList.push_back(wstring(strSource.begin()+nBeg, strSource.end()));
 85                 break;
 86             }
 87         }
 88     }
 89     return true;
 90 }
 91 //URL编码
 92 string UrlEncode(const string& strSrc)
 93 {
 94     string strDes;
 95     for ( size_t i=0; i<strSrc.size(); ++i )
 96     {
 97         BYTE ch=(BYTE)strSrc[i];
 98         if ( isalnum(ch) || ch=='-' || ch=='_' || ch=='.' || ch=='~' )
 99             strDes+=ch;
100         else if ( ch==' ' )
101             strDes+='+';
102         else
103         {
104             strDes+='%';
105             strDes+=ToHex( (ch>>4) );
106             strDes+=ToHex( ch%16 );
107         }
108     }
109     return strDes;
110 }
111 //URL解码
112 string UrlDecode(const string& strSrc)
113 {
114     string strDes;  
115     for ( size_t i = 0; i < strSrc.size(); i++ )  
116     {  
117         BYTE ch=strSrc[i];
118         if (ch == '+') 
119             strDes+=' ';  
120         else if (ch == '%')  
121         {  
122             BYTE h = FromHex((unsigned char)strSrc[++i]);  
123             BYTE l = FromHex((unsigned char)strSrc[++i]);  
124             strDes += (h<<4) + l;  
125         }  
126         else strDes += ch;  
127     }  
128     return strDes; 
129 }
130 //替换字符串
131 wstring StrReplaceW(const wstring& strContent, const wstring& strTag, const wstring& strReplace)
132 {
133     size_t nBegin=0, nFind=0;
134     nFind = strContent.find(strTag, nBegin);
135     if ( nFind == wstring::npos )
136         return strContent;
137     size_t nTagLen = strTag.size();
138     wstring strRet;
139     while ( true )
140     {
141         strRet.append(strContent.begin()+nBegin, strContent.begin()+nFind);
142         strRet.append(strReplace);
143         nBegin = nFind + nTagLen;
144         nFind = strContent.find(strTag, nBegin);
145         if ( nFind == wstring::npos )
146         {
147             strRet.append(strContent.begin()+nBegin, strContent.end());
148             break;
149         }
150     }
151     return strRet;
152 }
153 
154 string StrReplaceA( const string& strContent, const string& strTag, const string& strReplace )
155 {
156     size_t nBegin=0, nFind=0;
157     nFind = strContent.find(strTag, nBegin);
158     if ( nFind == string::npos )
159         return strContent;
160     size_t nTagLen = strTag.size();
161     string strRet;
162     while ( true )
163     {
164         strRet.append(strContent.begin()+nBegin, strContent.begin()+nFind);
165         strRet.append(strReplace);
166         nBegin = nFind + nTagLen;
167         nFind = strContent.find(strTag, nBegin);
168         if ( nFind == string::npos )
169         {
170             strRet.append(strContent.begin()+nBegin, strContent.end());
171             break;
172         }
173     }
174     return strRet;
175 }

转载:http://blog.csdn.net/mfcing/article/details/7529848

原文地址:https://www.cnblogs.com/chechen/p/5965151.html