vc2008 unicode环境 读GB312网页乱码
由于读出来以utf-8编码了,采用下面的读取方法(只想用这种方法),怎样将utf8转换成正常的中文显示,,
//////////////////////////
#import <msxml4.dll> named_guids
using namespace MSXML2;
CString resaa;
IXMLHTTPRequestPtr httpRes;
HRESULT hr=httpRes.CreateInstance("MSXML2.XMLHTTP");
if(!SUCCEEDED(hr))
{
AfxMessageBox("无法创建XMLHTTP对象,请检查是否安装了MS XML运行库!");
}
LPCTSTR url="http://localhost/changjun/asxml.asp";
httpRes->open("Get",url,false,"","");
httpRes->send();
if((httpRes->readyState)==4) //4时表示数据已加载完
{
resaa=httpRes->responseText.copy();
}
httpRes.Release();
由于读出来以utf-8编码了,采用下面的读取方法(只想用这种方法),怎样将utf8转换成正常的中文显示,,
//////////////////////////
#import <msxml4.dll> named_guids
using namespace MSXML2;
CString resaa;
IXMLHTTPRequestPtr httpRes;
HRESULT hr=httpRes.CreateInstance("MSXML2.XMLHTTP");
if(!SUCCEEDED(hr))
{
AfxMessageBox("无法创建XMLHTTP对象,请检查是否安装了MS XML运行库!");
}
LPCTSTR url="http://localhost/changjun/asxml.asp";
httpRes->open("Get",url,false,"","");
httpRes->send();
if((httpRes->readyState)==4) //4时表示数据已加载完
{
resaa=httpRes->responseText.copy();
}
httpRes.Release();
解决方案 »
- 怎样实现数据库的更新?(vs2005+sql2005 数据库:infobase 表:UserInfo(UserNum,UserName,Password,P))
- 如何快速有效的合并文件?
- 十六进制转换器的一个报错
- 怎样获得已知汉字(日文)的内码
- 关于ActiveX Control 中容纳的Dialog大小变化问题!
- vc中有队列类吗?
- help!如何在VC中用complex类?
- ?……
- COM和DCOM的区别可以这样理解吗?
- Programming Windows 95 with MFC 作者:Jeff Prosise
- DISP_FUNCTION(CTestWnd, "TestFunc", TestFunc, VT_BSTR, VTS_BSTR VTS_BSTR)
- MFC对话框如何实现自然书写
if(m_bUTF8)
{// utf8 file
MultiByteToWideChar(CP_UTF8,0,szBuf,-1,wBuf,2048);
WideCharToMultiByte(CP_ACP,0,wBuf,-1,szBuf,2048,0,0);
}
比如:
<script id="allmobilize" charset="utf-8" src="http://a.yunshipei.com/1327c36bdd7197e30fd9f4b48d1a5bcc/allmobilize.min.js"></script>
编码转换方法:
// 注释:多字节包括GBK和UTF-8
int GBK2UTF8(char *szGbk,char *szUtf8,int Len)
{
// 先将多字节GBK(CP_ACP或ANSI)转换成宽字符UTF-16
// 得到转换后,所需要的内存字符数
int n = MultiByteToWideChar(CP_ACP,0,szGbk,-1,NULL,0);
// 字符数乘以 sizeof(WCHAR) 得到字节数
WCHAR *str1 = new WCHAR[sizeof(WCHAR) * n];
// 转换
MultiByteToWideChar(CP_ACP, // MultiByte的代码页Code Page
0, //附加标志,与音标有关
szGbk, // 输入的GBK字符串
-1, // 输入字符串长度,-1表示由函数内部计算
str1, // 输出
n // 输出所需分配的内存
);
// 再将宽字符(UTF-16)转换多字节(UTF-8)
n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
if (n > Len)
{
delete[]str1;
return -1;
}
WideCharToMultiByte(CP_UTF8, 0, str1, -1, szUtf8, n, NULL, NULL);
delete[]str1;
str1 = NULL;
return 0;
} //UTF-8 GBK
int UTF82GBK(char *szUtf8,char *szGbk,int Len)
{
int n = MultiByteToWideChar(CP_UTF8, 0, szUtf8, -1, NULL, 0);
WCHAR * wszGBK = new WCHAR[sizeof(WCHAR) * n];
memset(wszGBK, 0, sizeof(WCHAR) * n);
MultiByteToWideChar(CP_UTF8, 0,szUtf8,-1, wszGBK, n);
n = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
if (n > Len)
{
delete[]wszGBK;
return -1;
}
WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGbk, n, NULL, NULL);
delete[]wszGBK;
wszGBK = NULL;
return 0;
}
UTF82GBK((LPSTR)(LPCTSTR)content,(LPSTR)(LPCTSTR)x,2048);
UTF82GBK((LPSTR)(LPCTSTR)resaa,(LPSTR)(LPCTSTR)x,2048);
看我的封装函数
std::string UnicodeToANSI( std::wstring str ,UINT CodePage)
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CodePage,
0,
str.c_str(),
-1,
NULL,
0,
NULL,
NULL ); pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
::WideCharToMultiByte(CodePage,
0,
str.c_str(),
-1,
pElementText,
iTextLen,
NULL,
NULL );
std::string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}
std::wstring ANSIToUnicode(std::string str ,UINT CodePage)
{
wchar_t* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = ::MultiByteToWideChar( CodePage,
0,
str.c_str(),
-1,
NULL,
NULL); pElementText = new wchar_t[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( wchar_t ) * ( iTextLen + 1 ) );
::MultiByteToWideChar( CodePage ,
0,
str.c_str(),
-1,
pElementText,
iTextLen);
std::wstring strText;
strText = pElementText;
delete[] pElementText;
return strText;
}
第二个参数:采用 CP_UTF8