UTF-8转GB2312函数
程序员文章站
2022-04-14 20:47:06
复制代码 代码如下:<% '用途:將utf-8編碼漢字轉換為gb2312碼,兼容英文和數字 '版權:雖說是原創,其實也參考了別人的部分算法&n...
复制代码 代码如下:
<%
'用途:將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
%>
'用途:將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
%>
推荐阅读
-
PHP截取中文字符串函数~包括GB2312和UT-8
-
php页面,mysql数据库转utf-8乱码,utf-8编码问题总结,mysqlutf-8
-
utf-8怎么转成gb2312
-
Thinkphp下面的xml_encode($data,encoding ='utf-8',root="think");函数怎么生成xml文件
-
php多维数组转一维数组的函数
-
10进制整数转62进制的函数
-
UTF-8转GBK编码问题,有什么好的解决方案?
-
转: SQLServer Case具有两种格式:简单Case函数和Case搜索函数
-
整型转字符串 判断是否为指定长度内字符串的php函数
-
PHP完美判断字符串是否为utf-8的函数_PHP教程