如何解码包含阿拉伯字符的(百分比编码 URL)?

Mah*_*ini 6 vb6 urlencode utf-8

我想转换所有语言的百分比编码 URL,但 vb6 仅支持英语。

我已经测试了以下代码。但它只能转换英文字符:

Private Sub Form_Load()
    THE_ARABIC_URL = "%D8%AF%D8%B4%D9%85%D9%86%DB%8C+%D8%AF%D8%B1+%D8%A7%D8%B9%D9%85%D8%A7%D9%82-2019-12-09+01%3A09%3A00"
    MsgBox URLDecode(THE_ARABIC_URL)
End Sub

Private Function URLDecode(ByVal txt As String) As String
    Dim txt_len As Integer
    Dim i As Integer
    Dim ch As String
    Dim digits As String
    Dim result As String

    result = ""
    txt_len = Len(txt)
    i = 1
    Do While i <= txt_len
        ' Examine the next character.
        ch = Mid$(txt, i, 1)
        If ch = "+" Then
            ' Convert to space character.
            result = result & " "
        ElseIf ch <> "%" Then
            ' Normal character.
            result = result & ch
        ElseIf i > txt_len - 2 Then
            ' No room for two following digits.
            result = result & ch
        Else
            ' Get the next two hex digits.
            digits = Mid$(txt, i + 1, 2)
            result = result & Chr$(CInt("&H" & digits))
            i = i + 2
        End If
        i = i + 1
    Loop

    URLDecode = result
End Function
Run Code Online (Sandbox Code Playgroud)

来源:VB 助手

Ahm*_*eed 5

如果您想手动执行此操作,则必须编写一个支持 UTF-8 的函数。然而,有一种更简单的方法,那就是使用MSScriptControl.ScriptControl对象来依赖 JScript 引擎。您可以使用此答案中的功能。

\n\n

这是一个完整的例子:

\n\n
Public JSEngine\n\nPublic Sub InitializeJSEngine()\n    Set JSEngine = CreateObject("MSScriptControl.ScriptControl")\n    JSEngine.Language = "JScript"\nEnd Sub\n\nFunction UrlDecode(s) As String\n    UrlDecode = Replace(s, "+", " ")\n    UrlDecode = JSEngine.CodeObject.decodeURIComponent(UrlDecode)\nEnd Function\n\nPrivate Sub Form_Load()\n    \' Make sure this is called before calling `UrlDecode`.\n    InitializeJSEngine\nEnd Sub\n\nPrivate Sub btnDecode_Click()\n    \' Prints: "\xd8\xaf\xd8\xb4\xd9\x85\xd9\x86\xd9\x8a \xd8\xaf\xd8\xb1 \xd8\xa7\xd8\xb9\xd9\x85\xd8\xa7\xd9\x82-2019-12-09 01:09:00"\n    \' ..which is Persian, not Arabic ;\xe2\x80\x91)\n    Debug.Print UrlDecode("%D8%AF%D8%B4%D9%85%D9%86%DB%8C+%D8%AF%D8%B1+%D8%A7%D8%B9%D9%85%D8%A7%D9%82-2019-12-09+01%3A09%3A00")\nEnd Sub\n
Run Code Online (Sandbox Code Playgroud)\n