Copy codeThe Code is as follows: <%
'Purpose: Write the UTF-8 into GB2312 compatible with English and numbers
'Authorization': it is the original example. In fact, some other algorithms are tested.
'Usage: Response. write UTF2GB ("% E9 % 83% BD % E5 % B8 % 82% E6 % 83% E7 % B7 % A3 % E6 % 85% 9F % E5 % BA % A7 ")
Function UTF2GB (UTFStr)
For Dig = 1 to len (UTFStr)
If mid (UTFStr, Dig, 1) = "%" then
If len (UTFStr)> = Dig + 8 then
GBStr = GBStr & ConvChinese (mid (UTFStr, Dig, 9 ))
Dig = Dig + 8
Else
GBStr = GBStr & mid (UTFStr, Dig, 1)
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 (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
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 ("& 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
Do
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
%>