一、GBK转换到Unicode编码

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;
}

二、Unicode转换到GBK编码

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;
}
05-21 17:41