1 void UTF8ToGB(CString &szOut) 2 { 3 WCHAR *strSrc; 4 TCHAR *szRes; 5 //长度 6 int i = MultiByteToWideChar(CP_UTF8, 0, szOut, -1, NULL, 0); 7 strSrc = new WCHAR[i+1]; 8 MultiByteToWideChar(CP_UTF8, 0, szOut, -1, strSrc, i); 9 //长度 10 i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL); 11 szRes = new TCHAR[i+1]; 12 WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL); 13 szOut = szRes; 14 delete []strSrc; 15 delete []szRes; 16 }
//GB转UTF8函数 void GBToUTF8(CString& strGBK) { int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0); WCHAR * wszUtf8 = new WCHAR[len+1]; memset(wszUtf8, 0, len * 2 + 2); MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len); len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL); char *szUtf8=new char[len + 1]; memset(szUtf8, 0, len + 1); WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL); strGBK = szUtf8; delete[] szUtf8; delete[] wszUtf8; }