decoding

S

shank

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
%>
 
D

Dan

shank said:
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 ;)
 
D

Dan

Dan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top