decoding

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

  1. shank

    shank Guest

    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
    #1
    1. Advertising

  2. shank

    Dan Guest

    "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
    #2
    1. Advertising

  3. shank

    Dan Guest

    "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
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ouadid abdelkarim
    Replies:
    0
    Views:
    2,009
    ouadid abdelkarim
    Jul 10, 2003
  2. Laurent Pinchart

    Address decoding

    Laurent Pinchart, Apr 11, 2004, in forum: VHDL
    Replies:
    3
    Views:
    6,348
    Jim Lewis
    Apr 12, 2004
  3. Bjorn Jensen
    Replies:
    2
    Views:
    829
    Bjorn Jensen
    Mar 22, 2005
  4. Martin Schimmelpfennig

    Data Decoding at 10 Gbit/s

    Martin Schimmelpfennig, Dec 21, 2005, in forum: VHDL
    Replies:
    4
    Views:
    2,670
    JosephKK
    Jan 2, 2006
  5. Slade

    Problem encoding/decoding image

    Slade, Jun 25, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,118
    Natty Gur
    Jun 25, 2003
Loading...

Share This Page