Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

decoding

0 views
Skip to first unread message

shank

unread,
Dec 9, 2009, 11:11:55 AM12/9/09
to
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
%>


Dan

unread,
Dec 10, 2009, 5:37:26 AM12/10/09
to

"shank" <sh...@tampabay.rr.com> wrote in message
news:#SMylpOe...@TK2MSFTNGP02.phx.gbl...

> 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

unread,
Dec 10, 2009, 6:39:45 AM12/10/09
to

"Dan" <ne...@worldofspack.com> wrote in message
news:38173CC2-65BE-4378...@microsoft.com...

>
> "shank" <sh...@tampabay.rr.com> wrote in message
> news:#SMylpOe...@TK2MSFTNGP02.phx.gbl...
>> 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

0 new messages