Velocity Reviews > decoding

# decoding

shank
Guest
Posts: n/a

 12-09-2009
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
Guest
Posts: n/a

 12-10-2009

"shank" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> 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...coded-url.html .
Don't always assume that everything you see on the web is correct

--
Dan

Dan
Guest
Posts: n/a

 12-10-2009

"Dan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "shank" <(E-Mail Removed)> wrote in message
> news:#(E-Mail Removed)...
>> 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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Aido Firefox 6 10-30-2005 03:19 AM murman Firefox 1 09-03-2004 07:47 PM Laurent Pinchart VHDL 3 04-12-2004 05:50 PM Joebee Firefox 0 09-24-2003 12:04 PM ouadid abdelkarim VHDL 0 07-10-2003 03:56 AM