Utf-8 to gb2312 and UrlEncode functions (fine code)

Source: Internet
Author: User
Tags exit chr end eval functions urlencode
function |utf-8|gb2312|urlencode

There are many urlencode related functions on the Internet, but there are many problems, there are UTF-8 to GB2312 or GB2312 to UTF-8 function is also a lot of problems, the following provides a common function is very useful, the proposed collection, in the processing of Chinese parameters is helpful.

The source code is as follows:

<%
-----------------------------------
' Utf-8-> gb2312
'-----------------------------------
function UTF2GB (UTFSTR)
Utfstr=replace (UTFSTR, "+", "%20")
For Dig=1 to Len (UTFSTR)
If mid (utfstr,dig,1) = "%" Then
If LCase (Mid (utfstr,dig+1,1)) = "E" Then
Gbstr=gbstr & Convchinese (Mid (utfstr,dig,9))
Dig=dig+8
Else
Gbstr=gbstr & Chr (eval ("&h" +mid (utfstr,dig+1,2))
Dig=dig+2
End If
Else
Gbstr=gbstr & Mid (utfstr,dig,1)
End If
Next
Utf2gb=gbstr
End Function

function Convchinese (x)
A=split (Mid (x,2), "%")
I=0
J=0

For i=0 to UBound (A)
A (i) =c16to2 (A (i))
Next

For i=0 to UBound (A)-1
Digs=instr (A (i), "0")
Unicode= ""
For J=1 to DigS-1
If J=1 Then
A (i) =right (A (i), Len (A (i))-digs)
Unicode=unicode & A (i)
Else
I=i+1
A (i) =right (A (i), Len (A (i))-2)
Unicode=unicode & A (i)
End If
Next

If Len (c2to16 (Unicode)) =4 Then
Convchinese=convchinese & ChrW (int ("&h" & C2to16 (Unicode))
Else
Convchinese=convchinese & chr (int ("&h" & C2to16 (Unicode))
End If
Next
End Function
function c2to16 (x)
I=1
For I=1 to Len (x) Step 4
C2to16=c2to16 & Hex (C2to10 (Mid (x,i,4)))
Next ' www.aspxuexi.com
End Function

function C2to10 (x)
C2to10=0
If x= "0" Then Exit function
I=0
For i= 0 to Len (x)-1
If Mid (X,len (x)-i,1) = "1" then c2to10=c2to10+2^ (i)
Next
End Function
function C16to2 (x)
I=0
For I=1 to Len (Trim (x))
Tempstr= C10to2 (CInt (int ("&h" & Mid (x,i,1)))
Do While Len (TEMPSTR) <4
Tempstr= "0" & TempStr
Loop
C16to2=c16to2 & TempStr
Next
End Function
function C10to2 (x)
MYSIGN=SGN (x)
X=abs (x)
Digs=1
Todo
If X<2^digs Then
Exit Do
Else
Digs=digs+1
End If
Loop
Tempnum=x

I=0
For I=digs to 1 step-1
If tempnum>=2^ (i-1) Then
tempnum=tempnum-2^ (i-1)
C10to2=c10to2 & "1"
Else
C10to2=c10to2 & "0"
End If
Next
If Mysign=-1 then c10to2= "-" & C10to2
End Function

-------------------------www.aspxuexi.com-----------

Function UrlDecode (ENSTR)
Dim destr
Dim c,i,v
Destr= ""
For I=1 to Len (ENSTR)
C=mid (enstr,i,1)
If c= "%" then
V=eval ("&h" +mid (enstr,i+1,2))
If v<128 Then
DESTR=DESTR&AMP;CHR (v)
I=i+2
Else
If Isvalidhex (Mid (enstr,i,3)) Then
If Isvalidhex (Mid (enstr,i+3,3)) Then
V=eval ("&h" +mid (enstr,i+1,2) +mid (enstr,i+4,2))
DESTR=DESTR&AMP;CHR (v)
I=i+5
Else
V=eval ("&h" +mid (enstr,i+1,2) +cstr (Hex (ASC (enstr,i+3,1)))
DESTR=DESTR&AMP;CHR (v)
I=i+3
End If
Else
Destr=destr&c
End If
End If
Else
If c= "+" then
destr=destr& ""
Else
Destr=destr&c
End If
End If
Next
Urldecode=destr
End Function

function Isvalidhex (str)
Dim c
Isvalidhex=true
Str=ucase (str)
If Len (str) <>3 then Isvalidhex=false:exit function
If left (str,1) <> "%" then Isvalidhex=false:exit function
C=mid (str,2,1)
If not ((c>= "0") and (c<= "9")) or ((c>= "A") and (c<= "Z")) then Isvalidhex=false:exit function
C=mid (str,3,1)
If not ((c>= "0") and (c<= "9")) or ((c>= "A") and (c<= "Z")) then Isvalidhex=false:exit function
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.