winsock 接收网页编码为utf8格式 的问题
我使用winsock获取一个网页源码,返回内容使用的 byte数组,但是怎么从byte数据转换成可以显示在vb窗体中的编码(应该是gb2312),也就是从 byte数组转成 utf8,,再从utf8转成gb2312
[解决办法]
给几个函数,具体哪个转哪个记不清了,哈哈哈
- VB code
Function bytes2BSTR(ByVal vIn As String) As String Dim strReturn As String Dim i As Integer Dim ThisCharCode As Byte, NextCharCode As Byte strReturn = "" For i = 1 To LenB(vIn) ThisCharCode = AscB(MidB(vIn, i, 1)) If ThisCharCode < &H80 Then strReturn = strReturn & Chr(ThisCharCode) Else NextCharCode = AscB(MidB(vIn, i + 1, 1)) strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode)) i = i + 1 End If Next bytes2BSTR = strReturn End Function
[解决办法]
- VB code
'模块中Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As LongPublic Function UTF8_Decode(bUTF8() As Byte) As String '二进制解析为UTF8 Dim lRet As Long Dim lLen As Long Dim lBufferSize As Long Dim sBuffer As String Dim bBuffer() As Byte lLen = UBound(bUTF8) + 1 If lLen = 0 Then Exit Function lBufferSize = lLen * 2 sBuffer = String$(lBufferSize, Chr$(0)) lRet = MultiByteToWideChar(65001, 0, VarPtr(bUTF8(0)), lLen, StrPtr(sBuffer), lBufferSize) If lRet <> 0 Then sBuffer = Mid$(sBuffer, 1, lRet) End If UTF8_Decode = sBufferEnd Function