|
|
Title | URL decode a string |
Description | This example shows how to URL decode a string in Visual Basic 6. |
Keywords | URL, encode, decode, code, string |
Categories | Strings, Utilities |
|
|
Function URLDecode examines each character in a string. If the character is a +, it replaces it with a space. Then if the character not %, the routine adds it to the result string.
If the character is % but is too close to the end of the string to be followed by two hexadecimal digits, the routine simply outputs % (this is probably an error in the encoded string).
Finally, if the character is % followed by two hexadecimal characters, the program replaces them with their ASCII equivalent.
For example, this string:
Some+test+%28text%29+with+wierd+%3B%2D%29+characters+%2B+more%21
becomes this:
Some test (text) with wierd ;-) characters + more!
|
|
' Convert a URL safe encoding into its original text.
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
SetSafeChars
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
|
|
|
|
|
|