GBK、UTF8、UNICODE编码转换

 string GBKToUTF8(const std::string& strGBK)
{
int nLen = MultiByteToWideChar(CP_ACP, , strGBK.c_str(), -, NULL, );
WCHAR * wszUTF8 = new WCHAR[nLen];
MultiByteToWideChar(CP_ACP, , strGBK.c_str(), -, wszUTF8, nLen); nLen = WideCharToMultiByte(CP_UTF8, , wszUTF8, -, NULL, , NULL, NULL);
char * szUTF8 = new char[nLen];
WideCharToMultiByte(CP_UTF8, , wszUTF8, -, szUTF8, nLen, NULL, NULL); std::string strTemp(szUTF8);
delete[]wszUTF8;
delete[]szUTF8;
return strTemp;
}
 string UTF8ToGBK(const std::string& strUTF8)
{
int nLen = MultiByteToWideChar(CP_UTF8, , strUTF8.c_str(), -, NULL, );
unsigned short * wszGBK = new unsigned short[nLen + ];
memset(wszGBK, , nLen * + );
MultiByteToWideChar(CP_UTF8, , strUTF8.c_str(), -, (LPWSTR)wszGBK, nLen); nLen = WideCharToMultiByte(CP_ACP, , (LPWSTR)wszGBK, -, NULL, , NULL, NULL);
char *szGBK = new char[nLen + ];
memset(szGBK, , nLen + );
WideCharToMultiByte(CP_ACP,, (LPWSTR)wszGBK, -, szGBK, nLen, NULL, NULL); std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}
 std::string Gbk2Unicode(std::string &strValue)
{
std::string strReturn;
unsigned char chTemp;
int nLength = strValue.length()*+;
WCHAR *pwchBuf = new WCHAR[nLength];
memset(pwchBuf, , sizeof(WCHAR) * nLength); MultiByteToWideChar(CP_ACP, , strValue.c_str(), -, (LPWSTR)pwchBuf, nLength); for (size_t i = ; i < wcslen(pwchBuf); i++)
{
strReturn += "\\u";
chTemp = *((unsigned char*)pwchBuf+i*+);
if(chTemp)
{
strReturn += m_clsmyOpenssl.char_to_hex( (unsigned char)(chTemp >> ) );
strReturn += m_clsmyOpenssl.char_to_hex( (unsigned char)(chTemp % ) );
}
chTemp = *((unsigned char*)pwchBuf+i*);
strReturn += m_clsmyOpenssl.char_to_hex( (unsigned char)(chTemp >> ) );
strReturn += m_clsmyOpenssl.char_to_hex( (unsigned char)(chTemp % ) );
}
delete[]pwchBuf;
pwchBuf = NULL;
return strReturn;
}
 std::string Unicode2GBK(std::string &strValue)
{
std::vector<std::string> vcString;
MyTools::SplitString(strValue, "\\u", vcString); wchar_t* pwBuf = new wchar_t[strValue.length() + ];
memset(pwBuf, , (strValue.length() + )* sizeof(wchar_t)); int j(); for(std::vector<std::string>::iterator it = vcString.begin(); it != vcString.end(); ++it)
{
if (it->empty())
{
continue;
}
unsigned short wcTmp = ;
unsigned char cTmp = ; //因为有中文字符混合ASSCII码情况,所以条件为k < it->length()
for(size_t k = ; k < it->length(); ++k)
{
cTmp = (unsigned char)(*it)[k]; if(cTmp <= '')//0x30~0x39 即0~9
{
wcTmp |= (cTmp & 0x0f) << (it->length() - k - ) * ;
}
else if(cTmp >= 'a')//0x61~7a 即a~z
{
wcTmp |= (cTmp - 0x57) << (it->length() - k - ) * ;
}
else//0x41~5a 即A~Z
{
wcTmp |= (cTmp - 0x37) << (it->length() - k - ) * ;
}
}
pwBuf[j++] = (wchar_t)wcTmp;
}
char *pDst = new char[strValue.length() + ];
memset(pDst, , (strValue.length() + ) * sizeof(char)); WideCharToMultiByte(CP_ACP, , pwBuf, -, (char*)pDst, strValue.length() - , NULL, NULL);
std::string strRet(pDst); delete[]pwBuf;
pwBuf= NULL; delete[]pDst;
pDst=NULL; return strRet;
}
上一篇:linux环境下mysql默认是区分表名大小写的


下一篇:poj1472[模拟题]