Home > Software engineering >  Vc2008 unicode environment read GB312 webpage utf-8 turn normal display in Chinese?
Vc2008 unicode environment read GB312 webpage utf-8 turn normal display in Chinese?

Time:09-22

Vc2008 unicode environment GB312 webpage read
Due to read out with utf-8 encoding, use the following reading method (just in this way), how to convert utf8 to normal, according to Chinese,
//////////////////////////
# import & lt; Msxml4. Dll> Named_guids
Using the namespace MSXML2;
Cstrings resaa;
IXMLHTTPRequestPtr httpRes;
Retrieves the hr=httpRes. CreateInstance (" MSXML2. XMLHTTP ");
if(! SUCCEEDED (hr))
{
AfxMessageBox (" unable to create the XMLHTTP object, please check whether the installation of the MS XML runtime!" );
}
LPCTSTR url="http://localhost/changjun/asxml.asp";
HttpRes - & gt; Open (" Get ", url, false, ""," ");
HttpRes - & gt; The send ();
If ((httpRes - & gt; ReadyState)==4)//4 represent data loaded after
{
Resaa=httpRes - & gt; The responseText. Copy ();
}
HttpRes. Release ();

CodePudding user response:

Cstrings into CStringA also not line

CodePudding user response:

Just want to use this method of reading, but read character conversion is always unsuccessful

CodePudding user response:

MultiByteToWideChar converting a

CodePudding user response:

MultiByteToWideChar make several times, didn't fix

CodePudding user response:

Utf-8 is MultiByte non-unicode so
If (m_bUTF8)
{//utf8 file
MultiByteToWideChar (CP_UTF8, 0, szBuf, 1, wBuf, 2048);
WideCharToMultiByte (wBuf CP_ACP, 0, 1, szBuf, 0, 2048);
}

CodePudding user response:

Web page encoding format, first read the web page content encoding format, and then by the format of the web page encoding formats to display properly with respect to ok,
For example:
<script id="allmobilize" charset="utf-8" SRC="http://a.yunshipei.com/1327c36bdd7197e30fd9f4b48d1a5bcc/allmobilize.min.js" & gt; </script>
Code conversion methods:
//comment: multibyte including GBK and utf-8 
Int GBK2UTF8 (char * szGbk, char * szUtf8, int Len)
{
//multibyte GBK first (CP_ACP or ANSI) into a wide character in utf-8 16
//get the converted, the required memory characters
Int n=MultiByteToWideChar (szGbk CP_ACP, 0, 1, NULL, 0).
//character number multiplied by sizeof (WCHAR) get bytes
WCHAR * str1=new WCHAR [sizeof (WCHAR) * n];
//conversion
MultiByteToWideChar (CP_ACP,//the MultiByte Code Page Code Page
0,//notations, related to the phonetic symbol
GBK szGbk,//input string
- 1,//input string length, 1 said calculated by the function inside the
Str1,//output
N/a/o the allocated memory required
);

//then wide character (UTF - 16) conversion multibyte (utf-8)
N=WideCharToMultiByte (str1 CP_UTF8, 0, 1, NULL, 0, NULL, NULL);
If (n & gt; Len)
{
The delete [] str1;
return -1;
}
WideCharToMultiByte (str1 CP_UTF8, 0, 1, szUtf8, n, NULL, NULL);
The delete [] str1;
Str1=NULL;

return 0;
}

//utf-8 GBK
Int UTF82GBK (char * szUtf8, char * szGbk, int Len)
{
Int n=MultiByteToWideChar (szUtf8 CP_UTF8, 0, 1, NULL, 0).
WCHAR * wszGBK=new WCHAR [sizeof (WCHAR) * n];
Memset (wszGBK, 0, sizeof (WCHAR) * n);
MultiByteToWideChar (szUtf8 CP_UTF8, 0, 1, wszGBK, n);

N=WideCharToMultiByte (wszGBK CP_ACP, 0, 1, NULL, 0, NULL, NULL);
If (n & gt; Len)
{
The delete [] wszGBK;
return -1;
}

WideCharToMultiByte (wszGBK CP_ACP, 0, 1, szGbk, n, NULL, NULL);

The delete [] wszGBK;
WszGBK=NULL;

return 0;
}

CodePudding user response:

refer to 6th floor u011391040 response:
page encoding format, first read the web page content encoding format, and then by the format of the web page encoding formats to display properly with respect to ok,
For example:
<script id="allmobilize" charset="utf-8" SRC="http://a.yunshipei.com/1327c36bdd7197e30fd9f4b48d1a5bcc/allmobilize.min.js" & gt; </script>
Code conversion methods:
//comment: multibyte including GBK and utf-8 
Int GBK2UTF8 (char * szGbk, char * szUtf8, int Len)
{
//multibyte GBK first (CP_ACP or ANSI) into a wide character in utf-8 16
//get the converted, the required memory characters
Int n=MultiByteToWideChar (szGbk CP_ACP, 0, 1, NULL, 0).
//character number multiplied by sizeof (WCHAR) get bytes
WCHAR * str1=new WCHAR [sizeof (WCHAR) * n];
//conversion
MultiByteToWideChar (CP_ACP,//the MultiByte Code Page Code Page
0,//notations, related to the phonetic symbol
GBK szGbk,//input string
- 1,//input string length, 1 said calculated by the function inside the
Str1,//output
N/a/o the allocated memory required
);

//then wide character (UTF - 16) conversion multibyte (utf-8)
N=WideCharToMultiByte (str1 CP_UTF8, 0, 1, NULL, 0, NULL, NULL);
If (n & gt; Len)
{
The delete [] str1;
return -1;
}
WideCharToMultiByte (str1 CP_UTF8, 0, 1, szUtf8, n, NULL, NULL);
The delete [] str1;
Str1=NULL;

return 0;
}

//utf-8 GBK
Int UTF82GBK (char * szUtf8, char * szGbk, int Len)
{
Int n=MultiByteToWideChar (szUtf8 CP_UTF8, 0, 1, NULL, 0).
WCHAR * wszGBK=new WCHAR [sizeof (WCHAR) * n];
Memset (wszGBK, 0, sizeof (WCHAR) * n);
MultiByteToWideChar (szUtf8 CP_UTF8, 0, 1, wszGBK, n);

N=WideCharToMultiByte (wszGBK CP_ACP, 0, 1, NULL, 0, NULL, NULL);
If (n & gt; Len)
{
The delete [] wszGBK;
return -1;
}

WideCharToMultiByte (wszGBK CP_ACP, 0, 1, szGbk, n, NULL, NULL);

The delete [] wszGBK;
WszGBK=NULL;

return 0;
}


This code can do it?nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related