We help IT Professionals succeed at work.

ASP URLDecode Alternative

garethtnash
garethtnash used Ask the Experts™
on
Hi Experts,

I'm using the script below in a classic ASP site to URLDecode -

However when i run this on a particular page i get the following error -

"Microsoft VBScript runtime error '800a0009'

Subscript out of range: '[number: 0]'

/Scripts/urldecode.asp, line 17
"
Is there an alternative to overcome this?

Many thanks
<%Function URLDecode(sConvert)
    Dim aSplit
    Dim sOutput
    Dim I
    If IsNull(sConvert) Then
       URLDecode = ""
       Exit Function
    End If
	
    ' convert all pluses to spaces
    sOutput = REPLACE(sConvert, "+", " ")
	
    ' next convert %hexdigits to the character
    aSplit = Split(sOutput, "%")
	
    If IsArray(aSplit) Then
      sOutput = aSplit(0)
      For I = 0 to UBound(aSplit) - 1
        sOutput = sOutput & _
          Chr("&H" & Left(aSplit(i + 1), 2)) &_
          Right(aSplit(i + 1), Len(aSplit(i + 1)) - 2)
      Next
    End If
	
    URLDecode = sOutput
End Function%>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ummm I dont use ASP or VB but what is "aSplit = Split(sOutput, "%") " i would have thought it should be aSplit = Split(sConvert, "%")

Sorry if my post was stupid!!
Also I googled abit and found this on a website : http://classicasp.aspfaq.com/general/how-do-i-decode-an-encoded-url.html

<% 
    Function URLDecode(str) 
        str = Replace(str, "+", " ") 
        For i = 1 To Len(str) 
            sT = Mid(str, i, 1) 
            If sT = "%" Then 
                If i+2 < Len(str) Then 
                    sR = sR & _ 
                        Chr(CLng("&H" & Mid(str, i+1, 2))) 
                    i = i+2 
                End If 
            Else 
                sR = sR & sT 
            End If 
        Next 
        URLDecode = sR 
    End Function 
 
    Function URLEncode(str) 
        URLEncode = Server.URLEncode(str) 
    End Function 
 
    str1 = "http://www.foo.com/blah.asp?foo=1 & 2 &g=0" 
    str2 = URLEncode(str1) 
    str3 = URLDecode(str2) 
    Response.Write(str1 & "<br>" & str2 & "<br>" & str3) 
%>

Open in new window

Author

Commented:
thank you
Top Expert 2005

Commented:
Glad you have a solution.  A tidbit of information I discovered when trying to find out why ASP did not have a URLDecode function....Request.Querystring("param") automatically performs a URLDecode.  Its buried deep in MSDN.

FWIW.