ASP character conversion: mutual conversion between UTF-8 and gb2312

Source: Internet
Author: User

UTF-8 to gb2312 Function

<%
'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
%>

Bytes -----------------------------------------------------------------------------------------------------------------------------

ASP encoding conversion function gb2312 conversion 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
Response. write ("<style type = 'text/CSS '> body, TD, textarea, fieldset, input, button {font-size: 12px; Background-color: # d4d0c8; color: # 5b5b5b;} A {color: # 5b5b5b;} textarea {SCROLLBAR-HIGHLIGHT-COLOR: # d4d0c8; color: #808080; Background-color: # d4d0c8; font-size: 12px; font-family: verdana, Arial, Helvetica, sans-serif; SCROLLBAR-SHADOW-COLOR: # d4d0c8; SCROLLBAR-3DLIGHT-COLOR: # d4d0c8; SCROLLBAR-TRACK-COLOR: # d4d0c8; SCROLLBAR-ARROW-COLOR: # d4d0c8; SCROLLBAR-BASE-COLOR: # d4d0c8; SCROLLBAR-DARKSHADOW-COLOR: # d4d0c8 ;}. noindium {font-family: verdana, Arial, Helvetica, sans-serif; font-size: 12px; color: #808080; Border: 0px solid # ffffff; Background-color: # d4d0c8; text-Decoration: none; Background-repeat: Repeat; Background-position: Top ;}. line {border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-top-style: none; border-right-style: none; border-bottom-style: solid; border-left-style: none; border-bottom-color: # aaaaaa ;}. xline {border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-top-style: none; border-right-style: none; border-bottom-style: dotted; border-left-style: none; border-bottom-color: # bbbbbb ;} </style> ") & vbcrlf
Response. Write ("<body onload =" "document. form1.a. Focus ();" ">") & vbcrlf
Action = request. querystring ("action ")
Select case action
Case "do"
Call doing ()
Case else
Call Mains ()
End select

'-----------------------------
Sub Mains ()
Response. Write "<fieldset style =" "width: 60%" "align =" "center" "> <legend> gb2312-Utf-8 </legend>" & vbcrlf
Response. Write ("<form name =" "form1" "method = post action = '? Action = do '> <Div align = center> ") & vbcrlf
Response. write ("<textarea name = 'A' style = 'width: 99% 'rows = '8' onkeyup =" "document. form1.submit (); "" onafterpaste = "" document. form1.submit (); ""> </textarea> </div> <br> ") & vbcrlf
Response. write ("<Div align = center> <textarea id = 'B' style = 'width: 99% 'rows = '8'> </textarea> <br> </div> ") & vbcrlf
Response. Write ("<br> <input type = 'submit 'value =' '> </form> </fieldset>") & vbcrlf
End sub
'-----------------------------
Sub doing ()
Response. Write "<fieldset style =" "width: 60%" "align =" "center" "> <legend> gb2312-Utf-8 </legend>" & vbcrlf
Response. Write ("<form name =" "form1" "method = post action = '? Action = do '> <Div align = center> ") & vbcrlf
Response. write ("<textarea name = 'A' style = 'width: 99% 'rows = '8' onkeyup =" "document. form1.submit (); "" onafterpaste = "" document. form1.submit (); ""> "& request. form ("A") & "</textarea> </div> <br>") & vbcrlf
Response. write ("<Div align = center> <textarea id = 'B' style = 'width: 99% 'rows = '8' onmouseover =" "If (this. value. length! = 0) {docopy ()} "" onfocus = "" If (this. value. length! = 0) {docopy ()} ""> </textarea> <br> </div> ") & vbcrlf
Response. Write ("<br> <input type = 'submit 'value =' '> </form> </fieldset>") & vbcrlf

Response. write ("<SCRIPT> document. getelementbyid ('B '). innerhtml = '"&server.html encode (chinese2unicode (request. form ("A") & "';") & vbcrlf
Response. Write ("function docopy () {") & vbcrlf
Response. Write ("document. getelementbyid ('B'). Select ();") & vbcrlf
Response. Write ("JS = Document. getelementbyid ('B'). createTextRange ();") & vbcrlf
Response. Write ("js.exe ccommand ('copy');") & vbcrlf
Response. Write ("document. getelementbyid ('A'). value =''; ") & vbcrlf
Response. Write ("document. form1.submit ();}") & vbcrlf
Response. Write ("</SCRIPT>") & vbcrlf

End sub

%>

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.