欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP关于编码的几个有用的函数小结(utf8)

程序员文章站 2022-06-21 22:39:34
1、'utf转gb---将utf8编码文字转换为gb编码文字 复制代码 代码如下: function utf2gb(utfstr) for dig=1 to len(utf...
1、'utf转gb---将utf8编码文字转换为gb编码文字
复制代码 代码如下:

function utf2gb(utfstr)

for dig=1 to len(utfstr)
'如果utf8编码文字以%开头则进行转换
if mid(utfstr,dig,1)="%" then
'utf8编码文字大于8则转换为汉字
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

'utf8编码文字将转换为汉字
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

2、'gb转utf8--将gb编码文字转换为utf8编码文字
复制代码 代码如下:

function toutf8(szinput)
dim wch, uch, szret
dim x
dim nasc, nasc2, nasc3
'如果输入参数为空,则退出函数
if szinput = "" then
toutf8 = szinput
exit function
end if
'开始转换
for x = 1 to len(szinput)
'利用mid函数分拆gb编码文字
wch = mid(szinput, x, 1)
'利用ascw函数返回每一个gb编码文字的unicode字符代码
'注:asc函数返回的是ansi 字符代码,注意区别
nasc = ascw(wch)
if nasc < 0 then nasc = nasc + 65536

if (nasc and &hff80) = 0 then
szret = szret & wch
else
if (nasc and &hf000) = 0 then
uch = "%" & hex(((nasc \ 2 ^ 6)) or &hc0) & hex(nasc and &h3f or &h80)
szret = szret & uch
else
'gb编码文字的unicode字符代码在0800 - ffff之间采用三字节模版
uch = "%" & hex((nasc \ 2 ^ 12) or &he0) & "%" & _
hex((nasc \ 2 ^ 6) and &h3f or &h80) & "%" & _
hex(nasc and &h3f or &h80)
szret = szret & uch
end if
end if
next

toutf8 = szret
end function

3、'gb转unicode---将gb编码文字转换为unicode编码文字
复制代码 代码如下:

function chinese2unicode(str)
dim i
dim str_one
dim str_unicode
if(isnull(str)) then
exit function
end if
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

4、'url解码
复制代码 代码如下:

function urldecode(enstr)
dim destr
dim c,i,v
destr=""
for i=1 to len(enstr)
c=mid(enstr,i,1)
if c="%" then
v=eval("&h"+mid(enstr,i+1,2))
if v<128 then
destr=destr&chr(v)
i=i+2
else
if isvalidhex(mid(enstr,i,3)) then
if isvalidhex(mid(enstr,i+3,3)) then
v=eval("&h"+mid(enstr,i+1,2)+mid(enstr,i+4,2))
destr=destr&chr(v)
i=i+5
else
v=eval("&h"+mid(enstr,i+1,2)+cstr(hex(asc(mid(enstr,i+3,1)))))
destr=destr&chr(v)
i=i+3
end if
else
destr=destr&c
end if
end if
else
if c="+" then
destr=destr&" "
else
destr=destr&c
end if
end if
next
urldecode=destr
end function

'判断是否为有效的十六进制代码
function isvalidhex(str)
dim c
isvalidhex=true
str=ucase(str)
if len(str)<>3 then isvalidhex=false:exit function
if left(str,1)<>"%" then isvalidhex=false:exit function
c=mid(str,2,1)
if not (((c>="0") and (c<="9")) or ((c>="a") and (c<="z"))) then isvalidhex=false:exit function
c=mid(str,3,1)
if not (((c>="0") and (c<="9")) or ((c>="a") and (c<="z"))) then isvalidhex=false:exit function
end function
%>