I encountered the problem when I made a small feature today. My current page is UTF-8 coding. Get the Chinese character encoding in Baidu Source URL may be under GB. After searching for a long time on the Internet Use XMLHTTP to remotely request it to get it again, but this will undoubtedly increase the execution efficiency of the code. Other better methods are not found, but this method must not be used immediately, so I finally thought of another method.
For example, such as "Chinese characters"
The encoding under UTF-8 is:%E6%B1%89%E5%AD%97
The encoding under GB2312 is:%BA%BA%D7%D6 D6
And my webpage is currently encoded in UTF-8, so how to convert these pure encoding string into Chinese characters?
First of all, the following function should be included in my page:
Function urldecode (enstr)
DIM DESTR, StrSpeCial
Dim c, i, v
Destr =
strspecial =!#$%& '()*+,. -_/:; <=> [Email Protect] [/]^{|} ~%
For i = 1 to len (ENSTR)
C = MID (ENSTR, I, 1)
If c =% then
v = Eval (& H+MID (ENSTR, I+1,2))
If Instr (Strspecial, CHR (V))> 0 THEN
Destr = Destr & CHR (V)
I = i+2
Else
V = Eval (& H+ MID (ENSTR, I+ 1,2)+ MID (ENSTR, I+ 4,2))
Destr = Destr & CHR (V)
I = i+5
End if
Else
If c =+ then
Destr = Destr &
Else
Destr = Destr & C
End if
End if
Next
UrlDecode = Destr
End function
The above function is a URL decoding function. The function below is a function that converts UTF-8 encoding into Chinese characters:
Function UTF2GB (UTFSTR)
For Dig = 1 to Len (UTFSTR)
'If UTF8 coding text starts with%, it will be converted
If MID (UTFSTR, Dig, 1) =% then
'UTF8 encoding text is greater than 8, then converted to Chinese characters
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
'UTF8 encoding text will be converted into Chinese characters
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 that
Convchinese = Convchinese & chrw (int (& h & c2TO16 (unicode))
Else
Convchinese = Convchinese & CHR (int (& H & C2TO16 (Unicode))
End if
Next
End function
'Binary code convert to hexadecimal code
Function C2TO16 (x)
i = 1
For i = 1 to len (x) Step 4
C2TO16 = C2TO16 & HEX (C2TO10 (MID (X, I, 4)))
Next
End function
'Binary code converts to decimal code
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
'Scripture code is converted to binary code
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
'Top code convert to binary code
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
On the normal UTF-8 page, we can use it directly:
Response.write UTF2GB (%E6%B1%89%E5%AD%97)
That is: UTF2GB (%E6%B1%89%E5%AD%97) = Chinese characters
But if the current UTF8 page obtains a GB coding (such as%BA%BA%D7%D6), how can we convert it into Chinese characters? At this time, the decoding function of the URLDECODE (ENSTR) above will be used above, but it cannot be used directly. Special attention should be paid to temporarily changing the coding mode of the current page. The application for examples are as follows:
<%
Session.codePage = 936 'forced conversion to under GB2312
Dim myKey: MyKey = URLDECODE (%BA%BA%D7%D6) 'GB cod string
Session.codePage = 65001 'back the page back to UTF-8 coding again
Response.write (myKey)
%>
At this time, I got Chinese characters. How about, simple, remember to follow this site next time-in the distance.