VS 2008 Unicode UTF8 互转

来源:互联网 发布:mysql text使用 编辑:程序博客网 时间:2024/04/30 16:36

亲测可用,备份留用。

wstring CCodec::UTF8ToUnicode( const string& str ){int  len = 0;len = str.length();int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,0,str.c_str(),-1,NULL,0 );  wchar_t *  pUnicode;  pUnicode = new  wchar_t[unicodeLen+1];  memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));  ::MultiByteToWideChar( CP_UTF8,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen );  wstring  rt;  rt = ( wchar_t* )pUnicode;delete  pUnicode; return  rt;  }char* CCodec::UnicodeToUtf8(const wchar_t* buf){char* result;int textlen = 0;textlen = WideCharToMultiByte( CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL );result =(char *)malloc((textlen+1)*sizeof(char));memset(result, 0, sizeof(char) * ( textlen + 1 ) );WideCharToMultiByte( CP_UTF8, 0, buf, -1, result, textlen, NULL, NULL );return result; }//GBK 转 UTF8void CCodec::ConvertGBKToUtf8(CString& strGBK){int len=::MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK.GetString(), -1, NULL,0);unsigned short * wszUtf8 = new unsigned short[len+1];memset(wszUtf8, 0, len * 2 + 2);::MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK.GetString(), -1, (LPWSTR)wszUtf8, len);len = ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);char *szUtf8=new char[len + 1];memset(szUtf8, 0, len + 1);::WideCharToMultiByte (CP_UTF8, 0, (LPWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL);strGBK = szUtf8;delete[] szUtf8;delete[] wszUtf8;}


0 0
原创粉丝点击