c++中char*wchar_t*stringwstring間の相互変換
string U2A(const wstring& str)//Unicode Ascii </span>
{
string strDes;
if ( str.empty() )
goto __end;
int nLen=::WideCharToMultiByte(CP_ACP, 0, str.c_str(), str.size(), NULL, 0, NULL, NULL);
if ( 0==nLen )
goto __end;
char* pBuffer=new char[nLen+1];
memset(pBuffer, 0, nLen+1);
::WideCharToMultiByte(CP_ACP, 0, 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, 0, str.c_str(), str.size(), NULL, 0);
if ( 0==nLen )
goto __end;
wchar_t* pBuffer=new wchar_t[nLen+1];
memset(pBuffer, 0, nLen+1);
::MultiByteToWideChar(CP_ACP, 0, 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, 0, wstrUnicode.c_str(), -1, NULL, 0, NULL, NULL);
char* pBuffer=new char[nLen+1];
pBuffer[nLen] = '\0';
nLen = WideCharToMultiByte(CP_UTF8, 0, wstrUnicode.c_str(), -1, 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, 0);
wchar_t* wstrBuf = new wchar_t[u16Len + 1];
::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 = 0;
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=0; 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>>4) );
strDes+=ToHex( ch%16 );
}
}
return strDes;
}
//URL
string UrlDecode(const string& strSrc)
{
string strDes;
for ( size_t i = 0; 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<<4) + l;
}
else strDes += ch;
}
return strDes;
}
//
wstring StrReplaceW(const wstring& strContent, const wstring& strTag, const wstring& strReplace)
{
size_t nBegin=0, nFind=0;
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=0, nFind=0;
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;
}
以前の1部はとても原始的なコードで、マルチスレッドではもう適用されません.注意:utf 8文字がascii文字を回転する場合、ここには個別の関数はありません.utf 8--->Unicode->asciiを実現するには、2つのステップが必要です.逆も同じです.
符号化の違いについては、簡単に言えば.Unicode符号化の1文字は2バイトを占め、1文字は2バイトを占めている.Ascii符号化は原始的な符号化方式であり、1文字に1バイト、1文字に2バイトを占有する.utf-8符号化は変長符号化とも呼ばれ、前の2つの符号化長は固定されており、utf-8符号化の1つの漢字が2バイトまたは3バイト、4バイトを占める可能性がある.
もう一つ説明したいのは、英語のアルファベットがこの3つの符号化の違いであり、アルファベット「a」を例にとると、Unicode符号化の2つのバイトは0 x 00 x 61である.ascii符号化の1バイトは0 x 61である.UTF-8符号化においても1バイト0 x 61である.実は、英語の文字はasciiとuft-8の符号化の中で完全に同じで、だから実際の開発の中で、もしあなたがasciiとutf-8の符号化の間で符号化を変える必要があるならば、また中がすべて英語であることを知っていて、符号化を変えないでください!トランスコードのいくつかの基本関数に加えて、URL符号化、復号、文字列置換の関数も付属しています.これらのコードは実際のプロジェクトで使用されているので、安定しているはずです.ご指摘を歓迎します.