求助MFC中遇到的编码问题
我用项目的是unicode,以下这段代码可以通过webbroswer得到网页的源代码
const int MAX_SIZE=1024*3;
IHTMLDocument2 *pHTMLDocument=NULL;
IPersistStreamInit *pPSI=NULL;
IStream *pStream=NULL;
HGLOBAL hHTMLText;
if (!(pHTMLDocument = (IHTMLDocument2*)m_web.get_Document()))
return;
if (pHTMLDocument->QueryInterface(&pPSI))
return;
hHTMLText = GlobalAlloc(GMEM_FIXED, MAX_SIZE);
CreateStreamOnHGlobal(hHTMLText, TRUE, &pStream);
pPSI->Save(pStream, FALSE);
//hHTMLText就是你要的源代码,在此操作 用char*指向
char *pchar = (char*) hHTMLText;
可以运行完之后发现得到的pchar是乱码 ,我看了下那网页的编码是gb2312的,不知道有没有影响,该怎么做才能不是乱码。。? 或者说换种方式得到源代码?
[解决办法]
utf8转unicode,wchar显示
[解决办法]
看看有没有帮助
http://blog.csdn.net/segen_jaa/article/details/7550317
[解决办法]
MultiByteToWideChar
[解决办法]
MultiByteToWideChar(CP_ACP, ...); // 转成Unicode编码
[解决办法]
仅供参考,尽管是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