= dig+8 then gbstr=gbstr & convchinese(mid(utfstr,dig,9)) dig=dig+8 else gbstr=gbstr & mid(utfstr,dig,1) end ifelse gbstr=gbstr & mid(utfstr,dig,1)end if next utf2gb=gbstrend functionfunction 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)-1digs=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 ifnextif len(c2to16(unicode))=4 then convchinese=convchinese & chrw(int("&h" & c2to16(unicode)))else convchinese=convchinese & chr(int("&h" & c2to16(unicode)))end if nextend functionfunction c2to16(x) i=1 for i=1 to len(x) step 4c2to16=c2to16 & hex(c2to10(mid(x,i,4))) nextend function function c2to10(x) c2to10=0 if x="0" then exit function i=0 for i= 0 to len(x) -1if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i) nextend functionfunction c16to2(x) i=0 for i=1 to len(trim(x))tempstr= c10to2(cint(int("&h" & mid(x,i,1))))do while len(tempstr)=2^(i-1) then tempnum=tempnum-2^(i-1) c10to2=c10to2 & "1" else c10to2=c10to2 & "0"end if next if mysign=-1 then c10to2="-" & c10to2end function[/code], - 三思随然 - 主动即自由,阅历即财富!简简单单,一切随然!" /> asp写的汉字转换utf-8及utf-8转gb2312的代码 - 三思随然

asp写的汉字转换utf-8及utf-8转gb2312的代码


chinese2unicode 为汉字转 UTF-8编码,utf2gb为UTF-8转换为汉字GB2312编码

function chinese2unicode(str)
dim i
dim str_one
dim str_unicode
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
response.write str_unicode
end function
    


'utf-8 to gb2312
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


[本日志由 随然 于 2009-06-03 09:21 AM 编辑]
上一篇: PHP中指定GB2312编码文字转换成UTF-8编码的类方法
下一篇: CODEPAGE=936和65001两种情况下UrlEncode和URLDecode算法详解
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: ASP 代码 编码
相关日志:
评论: 0 | 引用: 0 | 查看次数: 12669
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1 字 | UBB代码 关闭 | [img]标签 关闭