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();  

解决方案 »

  1.   

    CString 改成 CStringA 也不行
      

  2.   

    MultiByteToWideChar转换一下
      

  3.   

    MultiByteToWideChar 搞了好多次,没搞定
      

  4.   

    utf-8编码是MultiByte非Unicode 所以
    if(m_bUTF8)
    {// utf8 file
    MultiByteToWideChar(CP_UTF8,0,szBuf,-1,wBuf,2048);
    WideCharToMultiByte(CP_ACP,0,wBuf,-1,szBuf,2048,0,0);
    }
      

  5.   

    网页中有编码格式,先读取网页内容的编码格式,然后由网页的编码格式转成能正常显示的格式就ok了。
    比如:
     <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;  
    }  
      

  6.   

    CString  x;
    UTF82GBK((LPSTR)(LPCTSTR)content,(LPSTR)(LPCTSTR)x,2048);
      

  7.   

    CString  x;

    UTF82GBK((LPSTR)(LPCTSTR)resaa,(LPSTR)(LPCTSTR)x,2048);
      

  8.   

    你这个搞不定应该不是转换的问题了,看你所说的环境是unicode程序,那样的话你显示前只要拿到unicode编码的字符串就行了,没必要转换到ascii编码,你就把 UTF82GBK 这个函数的前半段拿来用就行了
      

  9.   

    很简单
    看我的封装函数
    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
      

  10.   

    CString resaa;  在unicode 编译是CStringW resaa=httpRes->responseText.copy();   copy() 格式是bstr  _bsr_t 格式下面试了几种方法还不成功
      

  11.   

    对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A