关于GB2312与UTF-8的转换

在修改一个别人的项目时候遇到的问题.人家那个全部都是GB的编码,还非让我用AJAX....UTF-8对AJAX的支持才好啊.在GB给UTF-8页AJAX传信息之后发现传过来的东西惨不忍睹.....在网上找了半天才找到这个解决办法...谢天谢地,总算解决....PS:以后再也不接修改别人项目的活了,看那种乱七八糟的代码真是一种折磨....-_-!
 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值