有乱码帮忙改正

x363635334 2014-03-16 12:14:01

HINTERNET hnetOpen,hnet1; wchar_t sLine[2048]; DWORD dwBytesRead;
hnetOpen=InternetOpen("RookIE/1.0",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
if(!hnetOpen) {InternetCloseHandle(hnetOpen);return false;}
hnet1=InternetOpenUrl(hnetOpen,"http://www.baidu.com",NULL,0,INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_PRAGMA_NOCACHE,0);
if(!hnet1) {InternetCloseHandle(hnetOpen);InternetCloseHandle(hnet1);return false;}
for(;;)
{
if(! InternetReadFile(hnet1,sLine,2047,&dwBytesRead) )
{
InternetCloseHandle(hnetOpen);InternetCloseHandle(hnet1);return false;
}
if(dwBytesRead) {sLine[dwBytesRead]='\0';}//sLine汉字是乱码
else break;
}
InternetCloseHandle(hnetOpen);InternetCloseHandle(hnet1); return true;
...全文
95 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-03-17
  • 打赏
  • 举报
回复
仅供参考,尽管是VB6
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
'常用的代码页:
const cpUTF8   =65001
const cpGB2312 =  936
const cpGB18030=54936
const cpUTF7   =65000
Function MultiByteToUTF16(UTF8() As Byte, CodePage As Long) As String
    Dim bufSize As Long
    bufSize = MultiByteToWideChar(CodePage, 0&, UTF8(0), UBound(UTF8) + 1, 0, 0)
    MultiByteToUTF16 = Space(bufSize)
    MultiByteToWideChar CodePage, 0&, UTF8(0), UBound(UTF8) + 1, StrPtr(MultiByteToUTF16), bufSize
End Function

Function UTF16ToMultiByte(UTF16 As String, CodePage As Long) As Byte()
    Dim bufSize As Long
    Dim arr() As Byte
    bufSize = WideCharToMultiByte(CodePage, 0&, StrPtr(UTF16), Len(UTF16), 0, 0, 0, 0)
    ReDim arr(bufSize - 1)
    WideCharToMultiByte CodePage, 0&, StrPtr(UTF16), Len(UTF16), arr(0), bufSize, 0, 0
    UTF16ToMultiByte = arr
End Function

Private Sub Command1_Click()
    MsgBox MultiByteToUTF16(UTF16ToMultiByte("ab中,c", cpUTF8), cpUTF8)
End Sub

IT挖矿工 2014-03-17
  • 打赏
  • 举报
回复
我也遇到过这个问题,给你个函数转一下就OK了

//	UTF-8转为UNICODE编码
	CString UTF8ToUnicode(const char* UTF8)
	{
		DWORD dwUnicodeLen;        //转换后Unicode的长度
		TCHAR *pwText;            //保存Unicode的指针
		CString strUnicode;        //返回值
		//获得转换后的长度,并分配内存
		dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,UTF8,-1,NULL,0);
		pwText = new TCHAR[dwUnicodeLen];
		if (!pwText)
		{
			return strUnicode;
		}
		//转为Unicode
		MultiByteToWideChar(CP_UTF8,0,UTF8,-1,(LPWSTR)pwText,dwUnicodeLen);
		//转为CString
		strUnicode.Format(_T("%s"),pwText);
		//清除内存
		delete []pwText;
		//返回转换好的Unicode字串
		return strUnicode;
	}

bobo_包子 2014-03-16
  • 打赏
  • 举报
回复
帮你查了下是MultiByteToWideChar
bobo_包子 2014-03-16
  • 打赏
  • 举报
回复
百度的http是使用utf-8传输的,所以你得到的是utf-8字符串 你需要用Utf8ToWideChar进行转换。 我不知道MSDN有没有这个函数,你自己找找看

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧