c++中char*\wchar_t*\string\wstring之间的相互转换

 string U2A(const wstring& str)//Unicode字符转Ascii字符
{
string strDes;
if ( str.empty() )
goto __end;
int nLen=::WideCharToMultiByte(CP_ACP, , str.c_str(), str.size(), NULL, , NULL, NULL);
if ( ==nLen )
goto __end;
char* pBuffer=new char[nLen+];
memset(pBuffer, , nLen+);
::WideCharToMultiByte(CP_ACP, , str.c_str(), str.size(), pBuffer, nLen, NULL, NULL);
pBuffer[nLen]='\0';
strDes.append(pBuffer);
delete[] pBuffer;
__end:
return strDes;
} wstring A2U(const string& str)//Ascii字符转
{
wstring strDes;
if ( str.empty() )
goto __end;
int nLen=::MultiByteToWideChar(CP_ACP, , str.c_str(), str.size(), NULL, );
if ( ==nLen )
goto __end;
wchar_t* pBuffer=new wchar_t[nLen+];
memset(pBuffer, , nLen+);
::MultiByteToWideChar(CP_ACP, , str.c_str(), str.size(), pBuffer, nLen);
pBuffer[nLen]='\0';
strDes.append(pBuffer);
delete[] pBuffer;
__end:
return strDes;
} string U2Utf(const wstring& wstrUnicode)//Unicode转utf8
{
string strRet;
if( wstrUnicode.empty() )
return strRet;
int nLen = WideCharToMultiByte(CP_UTF8, , wstrUnicode.c_str(), -, NULL, , NULL, NULL);
char* pBuffer=new char[nLen+];
pBuffer[nLen] = '\0';
nLen = WideCharToMultiByte(CP_UTF8, , wstrUnicode.c_str(), -, pBuffer, nLen, NULL, NULL);
strRet.append(pBuffer);
delete[] pBuffer;
return strRet;
} wstring Utf2U(const string &str)//utf8转Unicode
{
int u16Len = ::MultiByteToWideChar(CP_UTF8, NULL,str.c_str(),(int)str.size(), NULL, );
wchar_t* wstrBuf = new wchar_t[u16Len + ];
::MultiByteToWideChar(CP_UTF8, NULL, str.c_str(),(int)str.size(), wstrBuf, u16Len);
wstrBuf[u16Len] = L'\0';
wstring wStr;
wStr.assign(wstrBuf, u16Len);
delete [] wstrBuf;
return wStr;
}
//分割字符串
bool SplitString(const wstring& strSource,const wstring& strFlag, vector<wstring>& paramList)
{
if ( strSource.empty() || strFlag.empty() )
return false;
paramList.clear();
size_t nBeg = ;
size_t nFind = strSource.find(strFlag, nBeg);
if ( nFind == std::wstring::npos )
paramList.push_back(strSource);
else
{
while ( true )
{
if ( nFind != nBeg )
paramList.push_back(strSource.substr(nBeg, nFind-nBeg));
nBeg = nFind + strFlag.size();
if ( nBeg == strSource.size() )
break;
nFind = strSource.find(strFlag, nBeg);
if ( nFind == std::wstring::npos )
{
paramList.push_back(wstring(strSource.begin()+nBeg, strSource.end()));
break;
}
}
}
return true;
}
//URL编码
string UrlEncode(const string& strSrc)
{
string strDes;
for ( size_t i=; i<strSrc.size(); ++i )
{
BYTE ch=(BYTE)strSrc[i];
if ( isalnum(ch) || ch=='-' || ch=='_' || ch=='.' || ch=='~' )
strDes+=ch;
else if ( ch==' ' )
strDes+='+';
else
{
strDes+='%';
strDes+=ToHex( (ch>>) );
strDes+=ToHex( ch% );
}
}
return strDes;
}
//URL解码
string UrlDecode(const string& strSrc)
{
string strDes;
for ( size_t i = ; i < strSrc.size(); i++ )
{
BYTE ch=strSrc[i];
if (ch == '+')
strDes+=' ';
else if (ch == '%')
{
BYTE h = FromHex((unsigned char)strSrc[++i]);
BYTE l = FromHex((unsigned char)strSrc[++i]);
strDes += (h<<) + l;
}
else strDes += ch;
}
return strDes;
}
//替换字符串
wstring StrReplaceW(const wstring& strContent, const wstring& strTag, const wstring& strReplace)
{
size_t nBegin=, nFind=;
nFind = strContent.find(strTag, nBegin);
if ( nFind == wstring::npos )
return strContent;
size_t nTagLen = strTag.size();
wstring strRet;
while ( true )
{
strRet.append(strContent.begin()+nBegin, strContent.begin()+nFind);
strRet.append(strReplace);
nBegin = nFind + nTagLen;
nFind = strContent.find(strTag, nBegin);
if ( nFind == wstring::npos )
{
strRet.append(strContent.begin()+nBegin, strContent.end());
break;
}
}
return strRet;
} string StrReplaceA( const string& strContent, const string& strTag, const string& strReplace )
{
size_t nBegin=, nFind=;
nFind = strContent.find(strTag, nBegin);
if ( nFind == string::npos )
return strContent;
size_t nTagLen = strTag.size();
string strRet;
while ( true )
{
strRet.append(strContent.begin()+nBegin, strContent.begin()+nFind);
strRet.append(strReplace);
nBegin = nFind + nTagLen;
nFind = strContent.find(strTag, nBegin);
if ( nFind == string::npos )
{
strRet.append(strContent.begin()+nBegin, strContent.end());
break;
}
}
return strRet;
}

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

上一篇:Oracle的commit详解(转)


下一篇:Table '' is marked as crashed and should be repaired 解决方法