UTF-8转GB2312函数

2018-09-06 12:19

阅读:433

  复制代码 代码如下:<%
用途:將UTF-8編碼漢字轉換為GB2312碼,兼容英文和數字
版權:雖說是原創,其實也參考了別人的部分算法
用法:Response.writeUTF2GB(%E9%83%BD%E5%B8%82%E6%83%85%E7%B7%A3%E6%98%9F%E5%BA%A7)


functionUTF2GB(UTFStr)
forDig=1tolen(UTFStr)
ifmid(UTFStr,Dig,1)=%then
iflen(UTFStr)>=Dig+8then
GBStr=GBStr&ConvChinese(mid(UTFStr,Dig,9))
Dig=Dig+8
else
GBStr=GBStr&mid(UTFStr,Dig,1)
endif
else
GBStr=GBStr&mid(UTFStr,Dig,1)
endif
next
UTF2GB=GBStr
endfunction

functionConvChinese(x)
A=split(mid(x,2),%)
i=0
j=0

fori=0toubound(A)
A(i)=c16to2(A(i))
next

fori=0toubound(A)-1
DigS=instr(A(i),0)
Unicode=
forj=1toDigS-1
ifj=1then
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)
endif
next

iflen(c2to16(Unicode))=4then
ConvChinese=ConvChinese&chrw(int(&H&c2to16(Unicode)))
else
ConvChinese=ConvChinese&chr(int(&H&c2to16(Unicode)))
endif
next
endfunction

functionc2to16(x)
i=1
fori=1tolen(x)step4
c2to16=c2to16&hex(c2to10(mid(x,i,4)))
next
endfunction

functionc2to10(x)
c2to10=0
ifx=0thenexitfunction
i=0
fori=0tolen(x)-1
ifmid(x,len(x)-i,1)=1thenc2to10=c2to10+2^(i)
next
endfunction

functionc16to2(x)
i=0
fori=1tolen(trim(x))
tempstr=c10to2(cint(int(&h&mid(x,i,1))))
dowhilelen(tempstr)<4
tempstr=0&tempstr
loop
c16to2=c16to2&tempstr
next
endfunction

functionc10to2(x)
mysign=sgn(x)
x=abs(x)
DigS=1
do
ifx<2^DigSthen
exitdo
else
DigS=DigS+1
endif
loop
tempnum=x

i=0
fori=DigSto1step-1
iftempnum>=2^(i-1)then
tempnum=tempnum-2^(i-1)
c10to2=c10to2&1
else
c10to2=c10to2&0
endif
next
ifmysign=-1thenc10to2=-&c10to2
endfunction
%>


评论


亲,登录后才可以留言!