# decoding

Discussion in 'ASP General' started by shank, Dec 9, 2009.

1. ### shankGuest

Having trouble decoding. The below function Encodes this...
5031 S. DELPHIA ST. to this...
5031 S%2E DELPHIA ST%2E
No problem.

But when decoded, I get...
5031 S. DELPHIA ST2E

%2E is decoding to 2E at the end

What am I doing wrong?
thanks!

<%
'function for coding links to customer form
' this function also decodes
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
%>

shank, Dec 9, 2009

2. ### DanGuest

"shank" <> wrote in message
news:#...
> Having trouble decoding. The below function Encodes this...
> 5031 S. DELPHIA ST. to this...
> 5031 S%2E DELPHIA ST%2E
> No problem.
>
> But when decoded, I get...
> 5031 S. DELPHIA ST2E
>
> %2E is decoding to 2E at the end
>
> What am I doing wrong?
> thanks!

You're only decoding if i, the position of the % character, is less than 3
characters from the end of the string: i+2 < len(str) means that if
i=len(str)-2 the expression is false. So, the final %2E is changed to 2E
because if that expression is false, you're not adding the % to the new
string so it's dropped, but you're still adding the 2E afterwards.

Here's an adjusted version of your function that I think might work.

'function for coding links to customer form
' this function also decodes
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 'note change to <= here
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

Also see http://www.aspnut.com/reference/encoding.asp#urldecode for an
alternative method using an array split on the % boundaries.

Or http://flangy.com/dev/asp/urldecode.html which uses a regular expression

Both of these I haven't tested myself - I have no use for a URLDecode
function in my own ASP applications as wherever I have encoded URLs I tend
to use the Request.QueryString method to read the values which does the URL
decoding automatically.

I guess you got your function from here:
http://classicasp.aspfaq.com/general/how-do-i-decode-an-encoded-url.html .
Don't always assume that everything you see on the web is correct

--
Dan

Dan, Dec 10, 2009

3. ### DanGuest

"Dan" <> wrote in message
news:...
>
> "shank" <> wrote in message
> news:#...
>> Having trouble decoding. The below function Encodes this...
>> 5031 S. DELPHIA ST. to this...
>> 5031 S%2E DELPHIA ST%2E
>> No problem.
>>
>> But when decoded, I get...
>> 5031 S. DELPHIA ST2E
>>
>> %2E is decoding to 2E at the end
>>
>> What am I doing wrong?
>> thanks!

>
> You're only decoding if i, the position of the % character, is less than 3
> characters from the end of the string: i+2 < len(str) means that if
> i=len(str)-2 the expression is false

Just wanted to correct the above line, what I meant to write was that if
i=len(str)-3 then the expression is false; i+2 = len(str), therefore i+2 <
len(str) is false.

--
Dan

Dan, Dec 10, 2009