Implement your own urldecode using ASP applications

Source: Internet
Author: User

That is:
If there are spaces, replace % 20. If there are other characters, replace % ASCII. If there are four characters including Chinese characters, replace them with two % ASCII characters. However, sometimes we also need to decode the encoded string, but ASP does not provide related functions, which brings us some trouble to solve the problem. In fact, we only need to know the encoding rules, you can use ASP Code To implement our own urldecode function.
The specific implementation is as follows: Copy code The Code is as follows: function urldecode (encodestr)
Newstr = ""
Havechar = false
Lastchar = ""
For I = 1 to Len (encodestr)
Char_c = mid (encodestr, I, 1)
If char_c = "+" then
Newstr = newstr &""
Elseif char_c = "%" then
Next_shortc = mid (encodestr, I + 1, 2)
Next_1_num = CINT ("& H" & next_1_c)

If havechar then
Havechar = false
Newstr = newstr & CHR (CINT ("& H" & lastchar & next_0000c ))
Else
If ABS (next_1_num) <= 127 then
Newstr = newstr & CHR (next_1_num)
Else
Havechar = true
Lastchar = next_0000c
End if
End if
I = I + 2
Else
Newstr = newstr & char_c
End if
Next
Urldecode = newstr
End Function

The following provides a more mature function:Copy codeThe Code is as follows: '================================================ ==========
'Function name: urldecode
'Usage: URL Decoding
'================================================ ==========
Function urldecode (byval urlcode)
Dim start, final, length, Char, I, butf8, pass
Dim leftstr, rightstr, finalstr
Dim B0, B1, BX, blength, position, U, utf8
On Error resume next

B0 = array (0, 192,224,240,248,252,254)
Urlcode = Replace (urlcode, "+ ","")
Pass = 0
Utf8 =-1

Length = Len (urlcode): Start = instr (urlcode, "%"): final = faster Rev (urlcode, "% ")
If start = 0 or length <3 then urldecode = urlcode: Exit Function
Leftstr = left (urlcode, start-1): rightstr = right (urlcode, length-2-Final)

For I = start to final
Char = mid (urlcode, I, 1)
If char = "%" then
BX = urldecode_hex (mid (urlcode, I + 1, 2 ))
If BX> 31 and BX <128 then
I = I + 2
Finalstr = finalstr & chrw (BX)
Elseif BX & gt; 127 then
I = I + 2
If utf8 <0 then
Butf8 = 1: blength =-1: b1 = Bx
For position = 4 to 0 step-1
If B1> = b0 (position) and B1 <B0 (Position + 1) then
Blength = position
Exit
End if
Next
If blength>-1 then
For position = 0 to blength
B1 = urldecode_hex (mid (urlcode, I + position * 3 + 2 ))
If B1 <128 or B1> 191 then butf8 = 0: exit
Next
Else
Butf8 = 0
End if
If butf8 = 1 and blength = 0 then butf8 =-2
If butf8>-1 and utf8 =-2 then I = start-1: finalstr = "": Pass = 1
Utf8 = butf8
End if
If pass = 0 then
If utf8 = 1 then
B1 = Bx: u = 0: blength =-1
For position = 4 to 0 step-1
If B1> = b0 (position) and B1 <B0 (Position + 1) then
Blength = position
B1 = (b1 XOR B0 (position) * 64 ^ (Position + 1)
Exit
End if
Next
If blength>-1 then
For position = 0 to blength
BX = urldecode_hex (mid (urlcode, I + 2, 2): I = I + 3
If BX <128 or BX> 191 then u = 0: exit
U = u + (bx and 63) * 64 ^ (blength-position)
Next
If u> 0 then finalstr = finalstr & chrw (b1 + U)
End if
Else
B1 = Bx * & h100: u = 0
BX = urldecode_hex (mid (urlcode, I + 2, 2 ))
If BX> 0 then
U = b1 + bx
I = I + 3
Else
If left (urlcode, 1) = "%" then
U = b1 + ASC (mid (urlcode, I + 3, 1 ))
I = I + 2
Else
U = b1 + ASC (mid (urlcode, I + 1, 1 ))
I = I + 1
End if
End if
Finalstr = finalstr & CHR (u)
End if
Else
Pass = 0
End if
End if
Else
Finalstr = finalstr & char
End if
Next
Urldecode = leftstr & finalstr & rightstr
End Function

Function urldecode_hex (byval H)
On Error resume next
H = "& H" & trim (h): urldecode_hex =-1
If Len (h) <> 4 then exit function
If isnumeric (h) Then urldecode_hex = CINT (h)
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.