Lotus Notes Decode Web URL

Although we have heard of an @-function that will decode a URL, but it is undocumentend at this time. In the mean time, here is a generic LotusScript function that will decode any URL.

Update: We have been made aware that the older version of this function did not handle Unicode characters. They are encoded with a “%u” prefix and include 4 hex characters after the “%u”. The version below supports Unicode characters.

URL’s are encoded by replacing spaces with plus signs and replacing symbols with “%” followed by their 2 digit (hex) ASCII value. The character “%” is encoded to “%25″, so all “%” characters are encoded, and the “+” character is encoded to “%2B”, so all “+” characters are replaced spaces. Note that “%” can be used or “%x” to get the same result (%2B = %x2B = the + character). Instead of listing out all the possible characters in a big “case” statement, this function acts generically:

Function URLDecode(inpString As String) As String
Dim temp As String
Dim hexValue As String
Dim ch As String
Dim pos As Integer
Dim newPos As Integer
‘ First, replace any plus signs with spaces
temp = inpString
While Instr(temp, “+”) <> 0
temp = Left(temp, Instr(temp, “+”)-1) & ” ” & Mid(temp, Instr(temp, “+”)+1)
Wend
‘ Next, replace any “%x” encodings with the character
pos = 1
While Instr(pos, temp, “%x”) <> 0
hexValue = Mid(temp, Instr(pos, temp, “%x”)+2, 2)
ch = Chr$(Val(“&H” & hexValue))
newPos = Instr(pos, temp, “%x”)+2
temp = Left(temp, Instr(pos, temp, “%x”)-1) & ch & Mid(temp, Instr(pos, temp, “%x”)+4)
pos = newPos
Wend
‘ Next, replace any “%u” encodings with the Unicode character
pos = 1
While Instr(pos, temp, “%u”) <> 0
hexValue = Mid(temp, Instr(pos, temp, “%u”)+2, 4)    ‘ Unicode encodings are 4 hex characters
ch = Uchr$(Val(“&H” & hexValue))
newPos = Instr(pos, temp, “%u”)+2    ‘ Skip over so we don’t find “%” if that’s what it was decoded to
temp = Left(temp, Instr(pos, temp, “%u”)-1) & ch & Mid(temp, Instr(pos, temp, “%u”)+6)
pos = newPos
Wend
‘ Next, replace any “%” encodings with the character
pos = 1
While Instr(pos, temp, “%”) <> 0
hexValue = Mid(temp, Instr(pos, temp, “%”)+1, 2)
ch = Chr$(Val(“&H” & hexValue))
newPos = Instr(pos, temp, “%”)+1
temp = Left(temp, Instr(pos, temp, “%”)-1) & ch & Mid(temp, Instr(pos, temp, “%”)+3)
pos = newPos
Wend
URLDecode = temp
End Function

Viewed 8714 times by 2149 viewers

  1. Patrick Kwinten
    April 1st, 2010 at 18:26 | #1

    does the following function not do the same?

    Public Function urlDecode(s As String) As String
    If Len(s) = 0 Then Exit Function
    Dim i As Integer
    Dim tmp As String
    Dim c As String
    For i = 1 To Len(s)
    c = Mid$(s, i, 1)
    If c = “+” Then c = ” ”
    If c = “%” Then
    c = Chr$(“&H” + Mid$(s, i + 1, 2))
    i = i + 2
    End If
    tmp = tmp + c
    Next i
    urlDecode = tmp
    End Function

  1. No trackbacks yet.