zoukankan      html  css  js  c++  java
  • 解决Inet控件下载utf8网页乱码的问题

    Inet控件(Internet Transfer Control控件)下载网页的HTML代码是很方便,不过有个问题,在读取的是utf-8编码的网页时会出现乱码。这也难怪VB默认支持的是UNICODE编码,在读取utf-8的数据时自然不知所措了。

    (注:如果你还不知道上面所说的各种字符编码方式的意义的话,那您还是先阅读一下这篇文章:各种字符编码方式详解(ANSI,UNICODE,UTF-8,GB2312,GBK),如果您已经知道请继续)

    那怎么才能将utf-8的网页数据转换成UNICODE呢?
    首先Inet取得数据的时候必须用二进制的形式取得,比如:
    异步调用:
    Private Sub Command1_Click()
        Dim StrUrl As String
       
        StrUrl = Text1.Text
        Inet1.Execute StrUrl, "GET"
    End Sub

    Private Sub Inet1_StateChanged(ByVal State As Integer)
        If State = icResponseCompleted Then
            Dim BinBuff() As Byte
           
            BinBuff = Inet1.GetChunk(0, icByteArray)
        End If
    End Sub

    同步调用:
    Private Sub Command2_Click()
        Dim BinBuff() As Byte
        Dim StrUrl As String
       
        StrUrl = Text1.Text
        BinBuff = Inet1.OpenURL(Text1.Text, icByteArray)
        RichTextBox1.Text = Utf8ToUnicode(BinBuff)
    End Sub

    上面是取得inet取得二进制数据的代码

    添加模块
    'utf-8转换UNICODE代码
    Option Explicit

    Private 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 Long
    Private Const CP_UTF8 = 65001

    Function Utf8ToUnicode(ByRef Utf() As Byte) As String
        Dim lRet As Long
        Dim lLength As Long
        Dim lBufferSize As Long
        lLength = UBound(Utf) - LBound(Utf) + 1
        If lLength <= 0 Then Exit Function
        lBufferSize = lLength * 2
        Utf8ToUnicode = String$(lBufferSize, Chr(0))
        lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf(0)), lLength, StrPtr(Utf8ToUnicode), lBufferSize)
        If lRet <> 0 Then
            Utf8ToUnicode = Left(Utf8ToUnicode, lRet)
        Else
            Utf8ToUnicode = ""
        End If
    End Function

    Utf8ToUnicode函数即可将Inet收到的二进制数组转换成UNICODE字符串。传入参数为二进制数组返回转换后的字符串。
    上面的代码可为:
    Private Sub Inet1_StateChanged(ByVal State As Integer)
        If State = icResponseCompleted Then
            Dim BinBuff() As Byte
           
            BinBuff = Inet1.GetChunk(0, icByteArray)
            RichTextBox1.Text = Utf8ToUnicode(BinBuff)
        End If
    End Sub

    返回之后在RichTextBox1中显示,发现本来乱码的中文现在已经能正常显示了。

  • 相关阅读:
    模拟死锁
    B站学习斯坦福大学Swift 语言教程 iOS11 开发【第一集】踩到的几个坑(XCode 13.2.1版本)
    数学之美番外篇:平凡而又神奇的贝叶斯方法
    joj 1753: Street Numbers
    二叉树的三种遍历(递归+非递归)
    joj 1905: Freckles
    joj 2630: A Pair of Graphs(同构图的判定)
    vue3.x 中获取dom元素
    defineProperty 和 Proxy 的区别
    vue 按钮的防抖和节流
  • 原文地址:https://www.cnblogs.com/hackpig/p/1668510.html
Copyright © 2011-2022 走看看