当前位置: 移动技术网 > IT编程>开发语言>Asp > UTF-8 Unicode Ansi 汉字GB2321几种编码转换程序

UTF-8 Unicode Ansi 汉字GB2321几种编码转换程序

2017年12月12日  | 移动技术网IT编程  | 我要评论
今天搞sxna,遇到了编码转换的难题,搞了一个多小时,历尽千辛万苦,总算总结了如下的汉字转换为各种utf-8 unicode ansi编码的程序.不敢独享,特此奉献给各位编程爱好者!


--------------------------------------------------------------------------------
符合google的utf编码 
汉字 经过encodeuricomponent变成 %e6%b1%89%e5%ad%97 

%e6%b1%89%e5%ad%97经过decodeuricomponent变成汉字 

用google搜索"汉字":
http://www.google.com/search?hl=zh-cn&q=%e6%b1%89%e5%ad%97

符合baidu的utf-8编码 
汉字 经过 ansicode 变为 %ba%ba%d7%d6 

ba%ba%d7%d6 经过 decodeansi 变为 汉字 

用baidu搜索"汉字":
http://www.baidu.com/baidu?word=%ba%ba%d7%d6


--------------------------------------------------------------------------------

汉字经过chinese2unicode变为汉字

汉字经过utf2gb变为汉字


--------------------------------------------------------------------------------

test.asp 原程序如下
(下载源程序 )
复制代码 代码如下:

utf-8 unicode ansi 汉字gb2321几种编码转换程序

今天搞sxna,遇到了编码转换的难题,找了一个多小时,历尽千辛万苦,总算总结了如下的汉字转换为各种utf-8 unicode ansi编码的程序.不敢独享,特此奉献给各位编程爱好者!

<script language="javascript" runat="server">
var ss;
var dd;
ss=decodeuricomponent("%e6%b1%89%e5%ad%97");
dd=encodeuricomponent("汉字");
</script>
符合google的utf编码
<p>
汉字 经过encodeuricomponent变成
<%=dd%>
</p>
<p>
%e6%b1%89%e5%ad%97经过decodeuricomponent变成
<%=ss%>
</p>


<script language="vbscript" runat="server">
  function ansicode(vstrin)
    dim i, strreturn, innercode, thischr
    dim hight8, low8
    strreturn = "" 
    for i = 1 to len(vstrin) 
      thischr = mid(vstrin,i,1) 
      if abs(asc(thischr)) < &hff then 
        strreturn = strreturn & thischr 
      else
        innercode = asc(thischr)
        if innercode < 0 then
          innercode = innercode + &h10000
        end if
        hight8 = (innercode and &hff00) \ &hff
        low8 = innercode and &hff
        strreturn = strreturn & "%" & hex(hight8) & "%" & hex(low8)
      end if 
    next 
    ansicode = strreturn 
  end function

  function decodeansi(s)
    dim i, stmp, sresult, stmp1
    sresult = ""
    for i=1 to len(s)
      if mid(s,i,1)="%" then
        stmp = "&h" & mid(s,i+1,2)
        if isnumeric(stmp) then
          if cint(stmp)=0 then
            i = i + 2
          elseif cint(stmp)>0 and cint(stmp)<128 then
            sresult = sresult & chr(stmp)
            i = i + 2
          else
            if mid(s,i+3,1)="%" then
              stmp1 = "&h" & mid(s,i+4,2)
              if isnumeric(stmp1) then
                sresult = sresult & chr(cint(stmp)*16*16 + cint(stmp1))
                i = i + 5
              end if
            else
              sresult = sresult & chr(stmp)
              i = i + 2
            end if
          end if
        else
          sresult = sresult & mid(s,i,1)
        end if
      else
        sresult = sresult & mid(s,i,1)
      end if
    next
    decodeansi = sresult
  end function
</script>
符合baidu的utf-8编码
<p>
汉字 经过 ansicode 变为 
<%=ansicode("汉字")%>
</p>
<p>
%ba%ba%d7%d6 经过 decodeansi 变为 
<%=decodeansi("%ba%ba%d7%d6 ")%>
</p>

<%
'汉字转换为utf-8
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 
  chinese2unicode=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
%>
<p>汉字经过chinese2unicode变为
<%=chinese2unicode("汉字")%>(需要察看源代码,浏览器已经直接翻译)
</p>
<p>汉字经过utf2gb变为
<%=utf2gb("汉字")%>(需要察看源代码,浏览器已经直接翻译) 


另外在网上发现了一个不错的编码转换小站

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网