ASP字符转换:UTF-8与GB2312互转

时间:2023-01-06 00:03:44

UTF-8转GB2312函数

<%
'用途:將UTF-8編碼漢字轉換為GB2312碼,兼容英文和數字
'版權:雖說是原創,其實也參考了別人的部分算法
'用法:Response.write UTF2GB("%E9%83%BD%E5%B8%82%E6%83%85%E7%B7%A3 %E6%98%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)
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(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
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
%>

-----------------------------------------------------------------------------------------------------------------------------

ASP编码转换函数GB2312转换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(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;}.noinp {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& vbCrlf& 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.htmlencode(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.execCommand('Copy');")& vbCrlf
       Response.Write ("document.getElementById('a').value='';")& vbCrlf
       Response.Write ("document.form1.submit();}")& vbCrlf
       Response.Write ("</script>")& vbCrlf

End Sub

%>