vs2010 unicode环境。。下面这段代码乱码啊。。#include "iostream"
#define _AFXDLL
#define UNICODE
#define _UNICODE
#include "string"
#include "afx.h"
#include "afxinet.h"
using namespace std;string Unicode2Ansi(wchar_t* wstr) {
int len = WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wstr,-1,NULL,0,NULL,NULL);
char* tmps = new char[len];
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wstr,-1,tmps,len,NULL,NULL);
string ss = tmps;
delete [] tmps;
return ss;
}int main() {
CInternetSession sess(_TEXT(""));
CHttpFile* file = (CHttpFile*)sess.OpenURL(_TEXT("http://www.baidu.com"));
CString str;
while (file->ReadString(str)) {
cout << Unicode2Ansi(str.GetBuffer()) << endl;
}
file->Close();
cin.get();
return 0;
}我那么 转换编码不对吗? 应该怎么转换呢?我知道可以用ReadString的另一个重载达到 目的 ,但是我想知道怎么利用
WideCharToMutliByte函数来实现 。。
#define _AFXDLL
#define UNICODE
#define _UNICODE
#include "string"
#include "afx.h"
#include "afxinet.h"
using namespace std;string Unicode2Ansi(wchar_t* wstr) {
int len = WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wstr,-1,NULL,0,NULL,NULL);
char* tmps = new char[len];
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wstr,-1,tmps,len,NULL,NULL);
string ss = tmps;
delete [] tmps;
return ss;
}int main() {
CInternetSession sess(_TEXT(""));
CHttpFile* file = (CHttpFile*)sess.OpenURL(_TEXT("http://www.baidu.com"));
CString str;
while (file->ReadString(str)) {
cout << Unicode2Ansi(str.GetBuffer()) << endl;
}
file->Close();
cin.get();
return 0;
}我那么 转换编码不对吗? 应该怎么转换呢?我知道可以用ReadString的另一个重载达到 目的 ,但是我想知道怎么利用
WideCharToMutliByte函数来实现 。。
CString::GetBuffer函数参数不能为空,编译能通过?
MSDN:PXSTR GetBuffer(
int nMinBufferLength
);
PXSTR GetBuffer();
int main() {
CInternetSession sess(_TEXT(""));
CHttpFile* file = (CHttpFile*)sess.OpenURL(_TEXT("http://www.baidu.com"));
TCHAR buf[1024]= {0};
char tmp[2048] = {0};
while(file->ReadString(buf, 1024))
{
for(int i=0, j=0; i<_tcslen(buf); i++, j+=2)
{
tmp[j] = LOBYTE(buf[i]);
tmp[j+1] = HIBYTE(buf[i]);
}
cout<<tmp;
}
file->Close();
delete file;
file = NULL;
cin.get();
return 0;
}
因为和你的网页文件的编码有关系。
cout << Unicode2Ansi(str.GetBuffer()) << endl;
}这就不对了,你中间截取出来的,未必是完整的字符了。
CInternetSession sess(_TEXT(""));
CHttpFile* file = (CHttpFile*)sess.OpenURL(_TEXT("http://www.baidu.com"));
char tmp[2048] = {0};
while(file->ReadString((LPCSTR)tmp, 1024))
{
cout<<tmp;
}
file->Close();
delete file;
file = NULL;
cin.get();
return 0;
}