如何解决 VB 获取UTF8 显示乱码的问题
Private Sub Command1_Click()
ddd = "sex=1"
c = "POST " & page & " HTTP/1.1" & vbCrLf
c = c & "Content-Type: application/x-www-form-urlencoded " & vbCrLf
c = c & "Host: " & url & vbCrLf
c = c & "Content-Length: " & Len(ddd) & vbCrLf
c = c & vbCrLf
c = c & ddd
Winsock.RemoteHost = url
Winsock.RemotePort = 80
Winsock.protocol = sckTCPProtocol
Winsock.Connect
End Sub
Private Sub Winsock1_Connect()
Winsock1.SendData c
End Sub
Private Sub winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strResponse$
Winsock1.GetData strResponse, vbString, bytesTotal
On Error Resume Next
Text1.Text = strResponse
End Sub
这样 ,Text1.Text 中显示的就是乱码,有什么办法解决?百度 GOOGLE 2天了没满意答案
另外
1.必须用Winsock。
2.网页编码不能由我更改,那是别人的网页。
3.问这样的问题就说明我是新手啦,所以尽量详细哈。
晕,也许是我把问题说复杂了,1,2楼都答非所问,
简单来说把,
字符串变量strResponse 是从UTF8页面获取的中文字符串。如何将他转换为ANSII
和GBK码 展开
推荐于2016-07-08 · 知道合伙人软件行家
VB6.0可声明一个Object 数据类型,使用它的Charset属性为"UTF8",可解决TextBox控件中UTF8文本显示乱码问题。
Object 数据类型,Object 变量存储为 32 位(4
个字节)的地址形式,其为对象的引用。利用 Set 语句,声明为 Object
的变量可以赋值为任何对象的引用。Charset 属性,设置或者返回字体中所用字符集。
实现代码:
Private Sub Command1_Click() '打开UTF-8文本
Dim Ados As Object
CommonDialog1.FileName = ""
CommonDialog1.Filter = "文本文件(*.txt)|*.txt|"
CommonDialog1.Action = 1
str = CommonDialog1.FileName
Set Ados = CreateObject("adodb.stream")
With Ados
.Charset = "utf-8"
.Type = 2
.Open
.LoadFromFile str
Text1.Text = .ReadText
.Close
End With
Set Ados = Nothing
End Sub
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 Const CP_UTF8 = 65001
'Purpose:Convert Utf8 to Unicode
Public Function UTF8_Decode(ByVal sUTF8 As String) As String
Dim lngUtf8Size As Long
Dim strBuffer As String
Dim lngBufferSize As Long
Dim lngResult As Long
Dim bytUtf8() As Byte
Dim n As Long
If LenB(sUTF8) = 0 Then Exit Function
On Error GoTo EndFunction
bytUtf8 = StrConv(sUTF8, vbFromUnicode)
lngUtf8Size = UBound(bytUtf8) + 1
On Error GoTo 0
lngBufferSize = lngUtf8Size * 2
strBuffer = String$(lngBufferSize, vbNullChar)
'Translate using code page 65001(UTF-8)
lngResult = MultiByteToWideChar(CP_UTF8, 0, bytUtf8(0), _
lngUtf8Size, StrPtr(strBuffer), lngBufferSize)
'Trim result to actual length
If lngResult Then
UTF8_Decode = Left$(strBuffer, lngResult)
End If
EndFunction:
End Function
'把你上面获取的内容转一下就行,比如Text1.Text=UTF8_Decode(strResponse)
还可以加多一句InStr(1, strResponse, "charset=utf-8") > 0判断是否是UTF8编码再执行,不是UTF8的就直接显示。 还有些网页编码那里是大写的,要这样写InStr(1, strResponse, "charset=UTF-8") > 0
1.调用文件参数不正确(可以多试几次调用程序的参数类型)
2.调用文件返回值不正确(逐步调试)
3.子函数参数类型不正确。
总之一步一步的进行调试,看看究竟是哪一步出错就可以了。
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 Const CP_UTF8 = 65001
'Purpose:Convert Utf8 to Unicode
Public Function UTF8_Decode(ByVal sUTF8 As String) As String
Dim lngUtf8Size As Long
Dim strBuffer As String
Dim lngBufferSize As Long
Dim lngResult As Long
Dim bytUtf8() As Byte
Dim n As Long
If LenB(sUTF8) = 0 Then Exit Function
On Error GoTo EndFunction
bytUtf8 = StrConv(sUTF8, vbFromUnicode)
lngUtf8Size = UBound(bytUtf8) + 1
On Error GoTo 0
lngBufferSize = lngUtf8Size * 2
strBuffer = String$(lngBufferSize, vbNullChar)
'Translate using code page 65001(UTF-8)
lngResult = MultiByteToWideChar(CP_UTF8, 0, bytUtf8(0), _
lngUtf8Size, StrPtr(strBuffer), lngBufferSize)
'Trim result to actual length
If lngResult Then
UTF8_Decode = Left$(strBuffer, lngResult)
End If
EndFunction:
End Function
'把你上面获取的内容转一下就行,比如Text1.Text=UTF8_Decode(strResponse)
还可以加多一句InStr(1, strResponse, "charset=utf-8") > 0判断是否是UTF8编码再执行,不是UTF8的就直接显示。 还有些网页编码那里是大写的,要这样写InStr(1, strResponse, "charset=UTF-8") > 0