Today engaged in sxna, encountered a difficult problem of encoding conversion, engaged in more than an hour, have a great deal of hard work, finally summed up the following Chinese characters into a variety of UTF-8 Unicode Ansi encoding program. I am afraid to enjoy it exclusively. I would like to dedicate it to programmers!
--------------------------------------------------------------------------------
Complies with GOOGLE's UTF code
After the Chinese character passes through encodeURIComponent, it becomes % E6 % B1 % 89% E5 % AD % 97
% E6 % B1 % 89% E5 % AD % 97 After decodeURIComponent becomes a Chinese character
Search for "Chinese characters" by google ":
Http://www.google.com/search? Hl = zh-CN & q = % E6 % B1 % 89% E5 % AD % 97
Complies with BAIDU's UTF-8 code
The Chinese Character passes through AnsiCode to % BA % D7 % D6
BA % D7 % D6 changed to Chinese characters After DeCodeAnsi
Search for "Chinese characters" with baidu ":
Http://www.baidu.com/baidu? Word = % BA % D7 % D6
--------------------------------------------------------------------------------
Chinese characters are converted to Chinese characters through chinese2unicode
Convert Chinese characters to Chinese characters After UTF2GB
--------------------------------------------------------------------------------
The original program test. asp is as follows:
(Download source http://www.dc9.cn/upload/test.rar)Copy codeThe Code is as follows: UTF-8 Unicode Ansi Chinese GB2321 several encoding conversion programs
Today engaged in sxna, encountered a difficult problem of encoding conversion, looking for more than an hour, the past thousands of hardships, finally summed up the following Chinese characters into a variety of UTF-8 Unicode Ansi encoding program. I am afraid to enjoy it exclusively. I would like to dedicate it to programmers!
<Script language = "JAVASCRIPT" runat = "server">
Var ss;
Var dd;
Ss = decodeURIComponent ("% E6 % B1 % 89% E5 % AD % 97 ");
Dd = encodeURIComponent ("Chinese character ");
</Script>
Complies with GOOGLE's UTF code
<P>
EncodeURIComponent
<% = Dd %>
</P>
<P>
% E6 % B1 % 89% E5 % AD % 97 changed
<% = Ss %>
</P>
<Script language = "vbscript" runat = "server">
Function AnsiCode (vstrIn)
Dim I, strReturn, innerCode, ThisChr
Dim Hight8, Low8
StrReturn = ""
For I = 1 To Len (vstrIn)
ThisChr = Mid (vStrIn, I, 1)
If Abs (Asc (ThisChr) <& HFF Then
StrReturn = strReturn & ThisChr
Else
InnerCode = Asc (ThisChr)
If innerCode <0 Then
InnerCode = innerCode + & H10000
End If
Hight8 = (innerCode And & HFF00) \ & HFF
Low8 = innerCode And & HFF
StrReturn = strReturn & "%" & Hex (Hight8) & "%" & Hex (Low8)
End If
Next
AnsiCode = strReturn
End Function
Function DeCodeAnsi (s)
Dim I, sTmp, sResult, sTmp1
SResult = ""
For I = 1 To Len (s)
If Mid (s, I, 1) = "%" Then
STmp = "& H" & Mid (s, I + 1, 2)
If isNumeric (sTmp) Then
If CInt (sTmp) = 0 Then
I = I + 2
ElseIf CInt (sTmp)> 0 And CInt (sTmp) <128 Then
SResult = sResult & Chr (sTmp)
I = I + 2
Else
If Mid (s, I + 3, 1) = "%" Then
STmp1 = "& H" & Mid (s, I + 4, 2)
If isNumeric (sTmp1) Then
SResult = sResult & Chr (CInt (sTmp) * 16*16 + CInt (sTmp1 ))
I = I + 5
End If
Else
SResult = sResult & Chr (sTmp)
I = I + 2
End If
End If
Else
SResult = sResult & Mid (s, I, 1)
End If
Else
SResult = sResult & Mid (s, I, 1)
End If
Next
DeCodeAnsi = sResult
End Function
</Script>
Complies with BAIDU's UTF-8 code
<P>
The Chinese Character changes
<% = AnsiCode ("Chinese character") %>
</P>
<P>
% BA % D7 % D6 changed to DeCodeAnsi
<% = DeCodeAnsi ("% BA % D7 % D6") %>
</P>
<%
'Convert Chinese character to UTF-8
Function chinese2unicode (Str)
Dim I
Dim Str_one
Dim Str_unicode
For I = 1 to len (Str)
Str_one = Mid (Str, I, 1)
Str_unicode = Str_unicode & chr (38)
Str_unicode = Str_unicode & chr (35)
Str_unicode = Str_unicode & chr (1, 120)
Str_unicode = Str_unicode & Hex (ascw (Str_one ))
Str_unicode = Str_unicode & chr (59)
Next
Chinese2unicode = Str_unicode
End function
'Utf-8 To GB2312
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
%>
<P> the Chinese character is changed to chinese2unicode
<% = Chinese2unicode ("Chinese character") %> (you need to check the source code and the browser has translated it directly)
</P>
<P> after UTF2GB, the Chinese character is changed
<% = UTF2GB ("Chinese character") %> (you need to check the source code and the browser has translated it directly)
In addition, I found a good code conversion site on the Internet.
Http://www.dheart.net/bmzh/index.php