当前位置:首页 > ASP教程 > 应用技巧

实现UTF8转换GB2312国标码的asp代码

复制代码 代码如下:

'个人代码风格注释(变量名中第一个小写字母表表示变量类型)
'i:为integer型;
's:为string;
function u2utf8(byval a_inum)
dim sresult,sutf8
dim itemp,ihexnum,i

ihexnum = trim(a_inum)

if ihexnum = “” then
exit function
end if

sresult = “”

if (ihexnum < 128) then
sresult = sresult & ihexnum
elseif (ihexnum < 2048) then
sresult = chrb(&h80 + (ihexnum and &h3f))
ihexnum = ihexnum &h40
sresult = chrb(&hc0 + (ihexnum and &h1f)) & sresult
elseif (ihexnum < 65536) then
sresult = chrb(&h80 + (ihexnum and &h3f))
ihexnum = ihexnum &h40
sresult = chrb(&h80 + (ihexnum and &h3f)) & sresult
ihexnum = ihexnum &h40
sresult = chrb(&he0 + (ihexnum and &hf)) & sresult
end if

u2utf8 = sresult
end function

function gb2utf(byval a_sstr)
dim sgb,sresult,stemp
dim ilen,iunicode,itemp,i

sgb = trim(a_sstr)
ilen = len(sgb)
for i = 1 to ilen
stemp = mid(sgb,i,1)
itemp = asc(stemp)

if (itemp>127 or itemp<0) then
iunicode = ascw(stemp)
if iunicode<0 then
iunicode = iunicode + 65536
end if
else
iunicode = itemp
end if

sresult = sresult & u2utf8(iunicode)
next

gb2utf = sresult
end function

'调用方法
response.binarywrite(gb2utf(“中国人”))

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!