UTF8 转 UNICODE GBK ANSI

来源:互联网 发布:美工最好的网游 编辑:程序博客网 时间:2024/05/22 03:34

std::string UTF8ToGBK(const std::string& strUTF8)

{

       int len = MultiByteToWideChar(CP_UTF8,0,strUTF8.c_str(),-1,NULL,0);

       unsigned short *wszGBK = new unsigned short[len+1];

       memset(wszGBK,0,len*2+2);

       MultiByteToWideChar(CP_UTF8,0,(LPCTSTR)strUTF8.c_str(),-1,(LPWSTR)wszGBK,len);

       

      len = WideCharToMultiByte(CP_ACP,0,(LPCWSTR)wszGBK,-1,NULL,0,NULL,NULL);

      char *szGBK = new char[len+1];

      memset(szGBK,0,len+1);

      WideCharToMultiByte(CP_ACP,0,(LPCWSTR)wszGBK,-1,szGBK,len,NULL,NULL);

      //strUTF8 = szGBK;

      std::string strTemp(szGBK);

      delete[] szGBK;

      delete[] wszGBK;

      return strTemp;

}


CString UTF8ToGBK(CString strUTF8)

{

       TCHAR *wszPat;

       wszPat = (LPSTR)(LPCTSTR)strUTF8;

       int len ;

       //UTF-8转UNICODE

       len= MultiByteToWideChar(CP_UTF8,0,(LPCSTR)wszPat,-1,NULL,0);

       WCHAR *wszUtf8 = new WCHAR[len+1];

       memset(wszUtf8,0,len*2+2);

       MultiByteToWideChar(CP_UTF8,0,(LPCSTR)wszPat,-1,wszUtf8,len);

       //MessageBoxW(NULL,(const wchar_t*)wszUtf8,NULL,MB_OK);

      

      //UNICODE转ANSI,实际上经过两次转换,UTF-8已经变成了GBK编码  

      len = WideCharToMultiByte(CP_ACP,0,wszUtf8,-1,NULL,0,NULL,NULL);

      char *szGBK = new char[len+1];

      memset(szGBK,0,len+1);

      WideCharToMultiByte(CP_ACP,0,wszUtf8,-1,szGBK,len,NULL,NULL);

      CString strName;

      strName.Format("%s",szGBK);

      delete[] szGBK;

      delete[] wszUtf8;

      return strName;

      //AfxMessageBox((const char*)szGBK);

}



0 0
原创粉丝点击