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

时间:2019-12-09 14:11:24

标签: utf-8 vb6 urlencode

我想转换所有语言的百分比编码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

来源:VB Helper

2 个答案:

答案 0 :(得分:3)

如果要手动执行此操作,则必须编写具有UTF-8支持的函数。但是,有一种更简单的方法是使用MSScriptControl.ScriptControl对象依赖JScript引擎。您可以使用this answer中的函数。

这是一个完整的例子:

Public JSEngine

Public Sub InitializeJSEngine()
    Set JSEngine = CreateObject("MSScriptControl.ScriptControl")
    JSEngine.Language = "JScript"
End Sub

Function UrlDecode(s) As String
    UrlDecode = Replace(s, "+", " ")
    UrlDecode = JSEngine.CodeObject.decodeURIComponent(UrlDecode)
End Function

Private Sub Form_Load()
    ' Make sure this is called before calling `UrlDecode`.
    InitializeJSEngine
End Sub

Private Sub btnDecode_Click()
    ' Prints: "دشمني در اعماق-2019-12-09 01:09:00"
    ' ..which is Persian, not Arabic ;‑)
    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")
End Sub

答案 1 :(得分:0)

使用WSC在其他任何WSH中都具有64位支持:

此vbscript代码受@ kul-Tigin解决方案的启发,以便生成WITH data as ( SELECT 18 AS value, 1 AS id, "A" AS other_value, UNION ALL SELECT 20 AS value, 1 AS id, "B", UNION ALL SELECT 22 AS value, 2 AS id, "C" UNION ALL SELECT 30 AS value, 3 AS id, "A" UNION ALL SELECT 37 AS value, 2 AS id, "B" UNION ALL SELECT 31 AS value, 2 AS id, "C" UNION ALL SELECT 42 AS value, 1 AS id, "D" ), temp as ( select id, array_agg(struct(value, other_value)) as data from data group by id ) select id, array(select value from unnest(data)) as data, (select other_value from unnest(data) order by value ASC limit 1) first_other_data, (select other_value from unnest(data) order by value DESC limit 1) last_other_data from temp 并将其与相同的vbscript文件一起使用:

urlencdec.wsc