1 string GBKToUTF8(const std::string& strGBK) 2 { 3 int nLen = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0); 4 WCHAR * wszUTF8 = new WCHAR[nLen]; 5 MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, wszUTF8, nLen); 6 7 nLen = WideCharToMultiByte(CP_UTF8, 0, wszUTF8, -1, NULL, 0, NULL, NULL); 8 char * szUTF8 = new char[nLen]; 9 WideCharToMultiByte(CP_UTF8, 0, wszUTF8, -1, szUTF8, nLen, NULL, NULL); 10 11 std::string strTemp(szUTF8); 12 delete[]wszUTF8; 13 delete[]szUTF8; 14 return strTemp; 15 }
1 string UTF8ToGBK(const std::string& strUTF8) 2 { 3 int nLen = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0); 4 unsigned short * wszGBK = new unsigned short[nLen + 1]; 5 memset(wszGBK, 0, nLen * 2 + 2); 6 MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, (LPWSTR)wszGBK, nLen); 7 8 nLen = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL); 9 char *szGBK = new char[nLen + 1]; 10 memset(szGBK, 0, nLen + 1); 11 WideCharToMultiByte(CP_ACP,0, (LPWSTR)wszGBK, -1, szGBK, nLen, NULL, NULL); 12 13 std::string strTemp(szGBK); 14 delete[]szGBK; 15 delete[]wszGBK; 16 return strTemp; 17 }
1 std::string Gbk2Unicode(std::string &strValue) 2 { 3 std::string strReturn; 4 unsigned char chTemp; 5 int nLength = strValue.length()*4+1; 6 WCHAR *pwchBuf = new WCHAR[nLength]; 7 memset(pwchBuf, 0, sizeof(WCHAR) * nLength); 8 9 MultiByteToWideChar(CP_ACP, 0, strValue.c_str(), -1, (LPWSTR)pwchBuf, nLength); 10 11 for (size_t i = 0; i < wcslen(pwchBuf); i++) 12 { 13 strReturn += "\\u"; 14 chTemp = *((unsigned char*)pwchBuf+i*2+1); 15 if(chTemp) 16 { 17 strReturn += m_clsmyOpenssl.char_to_hex( (unsigned char)(chTemp >> 4) ); 18 strReturn += m_clsmyOpenssl.char_to_hex( (unsigned char)(chTemp % 16) ); 19 } 20 chTemp = *((unsigned char*)pwchBuf+i*2); 21 strReturn += m_clsmyOpenssl.char_to_hex( (unsigned char)(chTemp >> 4) ); 22 strReturn += m_clsmyOpenssl.char_to_hex( (unsigned char)(chTemp % 16) ); 23 } 24 delete[]pwchBuf; 25 pwchBuf = NULL; 26 return strReturn; 27 }
1 std::string Unicode2GBK(std::string &strValue) 2 { 3 std::vector<std::string> vcString; 4 MyTools::SplitString(strValue, "\\u", vcString); 5 6 wchar_t* pwBuf = new wchar_t[strValue.length() + 1]; 7 memset(pwBuf, 0, (strValue.length() + 1)* sizeof(wchar_t)); 8 9 int j(0); 10 11 for(std::vector<std::string>::iterator it = vcString.begin(); it != vcString.end(); ++it) 12 { 13 if (it->empty()) 14 { 15 continue; 16 } 17 unsigned short wcTmp = 0; 18 unsigned char cTmp = 0; 19 20 //因为有中文字符混合ASSCII码情况,所以条件为k < it->length() 21 for(size_t k = 0; k < it->length(); ++k) 22 { 23 cTmp = (unsigned char)(*it)[k]; 24 25 if(cTmp <= ‘9‘)//0x30~0x39 即0~9 26 { 27 wcTmp |= (cTmp & 0x0f) << (it->length() - k - 1) * 4; 28 } 29 else if(cTmp >= ‘a‘)//0x61~7a 即a~z 30 { 31 wcTmp |= (cTmp - 0x57) << (it->length() - k - 1) * 4; 32 } 33 else//0x41~5a 即A~Z 34 { 35 wcTmp |= (cTmp - 0x37) << (it->length() - k - 1) * 4; 36 } 37 } 38 pwBuf[j++] = (wchar_t)wcTmp; 39 } 40 char *pDst = new char[strValue.length() + 1]; 41 memset(pDst, 0, (strValue.length() + 1) * sizeof(char)); 42 43 WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, (char*)pDst, strValue.length() - 1, NULL, NULL); 44 std::string strRet(pDst); 45 46 delete[]pwBuf; 47 pwBuf= NULL; 48 49 delete[]pDst; 50 pDst=NULL; 51 52 return strRet; 53 }
原文地址:http://www.cnblogs.com/dongsheng/p/4387069.html
原文地址:https://www.cnblogs.com/wodehao0808/p/8616807.html
时间: 2024-11-10 08:20:08