6

I want to convert percent-encoding URLs in all languages but vb6 only supports English.

I have tested the following code. but it can only convert English characters:

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

Source: VB Helper.

Mahdi Jazini
  • 791
  • 9
  • 16
  • 1
    VB6 does **not** only support English. VB6 is Unicode internally. **1.** You are using wrong functions. You are working in ANSI. Use `MidW` not `Mid` - see help. **2**. VB6 talks to the UI via ANSI (or DBCS foir complex scripts), therefore system settings must match your language. The unicode gets converted to the system code page. Trying to print a Unicode string with Arabic in an English Windows code page won't work. –  Dec 09 '19 at 18:21

2 Answers2

4

If you want to do it manually, you'll have to write a function with UTF-8 support. However, there's an easier way which is to rely on the JScript engine using an MSScriptControl.ScriptControl object. You could use the function from this answer.

Here's a complete example:

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
    @MahdiJazini You're welcome! Side note regarding your suggested edit: you don't really need to reference "Microsoft Script Control" as long as you use late binding (i.e., `CreateObject("MSScriptControl.ScriptControl")`). You do, however, need the reference if you decided to go with early binding (i.e., `New MSScriptControl.ScriptControl`). – 41686d6564 stands w. Palestine Dec 09 '19 at 15:05
0

With 64-bit support in any other WSH using a WSC:

This vbscript code is inspired by @kul-Tigin solution in order to generate the urlencdec.wsc and use it with the same vbscript file as well :

'Question : Decoding URL encoded UTF-8 strings in VBScript
'URL : https://stackoverflow.com/questions/17880395/decoding-url-encoded-utf-8-strings-in-vbscript?answertab=active#tab-top

Option Explicit
Dim JSEngine,ws,WSC
Set ws = CreateObject("WScript.Shell")
WSC = ws.ExpandEnvironmentStrings("%AppData%\urlencdec.wsc")
Call Create_URL_ENC_DEC_Component(WSC)
Set JSEngine = GetObject("Script:"& WSC)

WScript.Echo JSEngine.decode("%D9%81%D9%8A%D9%84%D9%85-21Bridges-2019-%D9%85%D8%AA%D8%B1%D8%AC%D9%85")
WScript.Echo JSEngine.decode("%D9%81%D9%8A%D9%84%D9%85-Dolittle-2020-%D9%85%D8%AA%D8%B1%D8%AC%D9%85")


Sub Create_URL_ENC_DEC_Component(WSC)
Dim fso,File
Set fso = CreateObject("Scripting.FileSystemObject")
Set File = fso.OpenTextFile(WSC,2,True)
File.WriteLine "<?xml version=""1.0""?>"
File.WriteLine "<component>"
File.WriteLine "<?component error=""true"" debug=""true""?>"
File.WriteLine     "<registration"
File.WriteLine         "description=""Url Encode / Decode Helper"""
File.WriteLine         "progid=""JSEngine.Url"""
File.WriteLine         "version=""1.0"""
File.WriteLine         "classid=""{80246bcc-45d4-4e92-95dc-4fd9a93d8529}"""
File.WriteLine     "/>"
File.WriteLine    "<public>"
File.WriteLine         "<method name=""encode"">"
File.WriteLine             "<PARAMETER name=""s""/>"
File.WriteLine         "</method>"
File.WriteLine         "<method name=""decode"">"
File.WriteLine             "<PARAMETER name=""s""/>"
File.WriteLine         "</method>"
File.WriteLine     "</public>"
File.WriteLine     "<script language=""JScript"">"
File.WriteLine     "<![CDATA["
File.WriteLine         "var description = new UrlEncodeDecodeHelper;"
File.WriteLine         "function UrlEncodeDecodeHelper() {"
File.WriteLine             "this.encode = encode;"
File.WriteLine             "this.decode = decode;"
File.WriteLine         "}"
File.WriteLine         "function encode(s) {"
File.WriteLine            "return encodeURIComponent(s).replace(/'/g,""%27"").replace(/""/g,""%22"");"
File.WriteLine         "}"
File.WriteLine         "function decode(s) {"
File.WriteLine             "return decodeURIComponent(s.replace(/\+/g,  "" ""));"
File.WriteLine         "}"
File.WriteLine     "]]>"
File.WriteLine     "</script>"
File.WriteLine "</component>"
End Sub
Hackoo
  • 18,337
  • 3
  • 40
  • 70