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

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

程序员文章站 2022-06-05 12:25:40
复制代码 代码如下: '个人代码风格注释(变量名中第一个小写字母表表示变量类型) 'i:为integer型; 's:为string; function u2utf8(byv...
复制代码 代码如下:

'个人代码风格注释(变量名中第一个小写字母表表示变量类型)
'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(“中国人”))