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

ASP实现GB2312字符与区位码的相互转换的代码

程序员文章站 2023-11-13 15:26:16
研究编码,得知gb2312编码与区位码的关系,尝试之后,得此程序。 搜索,似乎没人写,故发此地。 原创首发: http://bbs.blueidea.com http://...
研究编码,得知gb2312编码与区位码的关系,尝试之后,得此程序。
搜索,似乎没人写,故发此地。
原创首发:
http://bbs.blueidea.com
http://mytju.com/classcode/
任意转载,任意使用。
1.简述
(1)gb2312标准的定义,其实就是区位码。
共94行,94列,行就是区号,列就是位号。
如“啊”字区号为16,位号为01,它的区位码就是1601。
(2)每个字符由区号+位号组成,共占两个字节。
每个字节都是01-94,与通信控制符0-31冲突,
所以,将区号和位号分别加上32,以避免冲突。
(3)由上,每个字节是33-126,与ascii编码0-127冲突,
所以将最高位置为1,也就是加上128,以避免冲突。
所以,最终,每个字节为161-254。
2。实现
原理很简单,加加减减即可实现。
直接将我完成的函数帖于此处。

复制代码 代码如下:

'----取得区位码的函数--------------------- 
function chartoqwm(byval str) 
    dim shex,shigh,slow,ilow,ihigh,sresult 
    shex=hex(asc(str)) '取得字符的内码的编码,如b0a1,此编码是正确的顺序,不必交换高低位。 
    shigh=left(shex,2) '取得编码的高位,如b0。 
    slow=right(shex,2) '取得编码的低位,如a1。 
    'gb2312内码范围为&ha1a1--&hfefe,每个字节都在a1-fe之间。 
    if not (shigh>="a1" and shigh<="fe") then 
        chartoqwm="" 
        exit function 
    end if 
    if not (slow>="a1" and slow<="fe") then 
        chartoqwm="" 
        exit function 
    end if 
    'gb交换码仅使用了7位,高位置1,即为内码。反过来就是将高位置0,可得到交换码。 
    ilow=clng("&h" & slow)-128  
    ihigh=clng("&h" & shigh)-128 
    '区位码与控制码0-31冲突,所以加上32之后,即是交换码。反过来减去32即可。 
    ilow=ilow-32 
    ihigh=ihigh-32 
    'ok,区位码已经得到。 
    sresult="" 
    if ihigh<10 then 
        sresult = sresult & "0" & cstr(ihigh) 
    else 
        sresult = sresult & cstr(ihigh) 
    end if 
    if ilow<10 then 
        sresult = sresult & "0" & cstr(ilow) 
    else 
        sresult = sresult & cstr(ilow) 
    end if 
    chartoqwm=sresult 
end function 
'----根据区位码得到字符的函数--------------------- 
function qwmtochar(byval str,byval docheckflg) 
    dim shex,shigh,slow,ilow,ihigh,sresult 
    '-------------检查输入格式-------------- 
    if docheckflg then 
        if len(str)<>4 then 
            qwmtochar="" 
            exit function 
        end if 
        '--4位必须都是数字 
        dim i,iasc 
        for i=1 to 4 
            iasc=asc(mid(str,i,1)) 
            if not (iasc>=&h30 and iasc<=&h39) then 
                qwmtochar="" 
                exit function 
            end if 
        next 
        '--区号,位号都要在01-94之间 
        ihigh=clng(left(str,2)) 
        ilow=clng(right(str,2)) 
        if not (ihigh>=1 and ihigh<=94) then 
            qwmtochar="" 
            exit function 
        end if 
        if not (ilow>=1 and ilow<=94) then 
            qwmtochar="" 
            exit function 
        end if 
    end if 
    '-------------检查完毕------------------ 
    ihigh=clng(left(str,2)) 
    ilow=clng(right(str,2)) 
    ihigh=ihigh + 32 + 128 
    ilow=ilow + 32 + 128 
    shex=hex(ihigh) & hex(ilow) 
    qwmtochar=chr("&h" & shex) 
end function 

使用方法:
-----------------------------------------------------------------------------------------------------
复制代码 代码如下:

dim i,str,schar 
str="娃哈哈" 
for i=1 to len(str) 
       schar=mid(str,i,1)  
       response.write schar & ":" & chartoqwm(schar) &"<br>" 
next 
----------------------------------------------------------------------------------------------------- 
dim str 
str="1601|1602|1603} 
if instr(str,"|")>0 then 
       dim s,schararray,i 
       schararray=split(str,"|") 
       for i=0 to ubound(schararray) 
              s=s & qwmtochar(trim(schararray(i)),true) 
       next 
       str=s 
else 
       str=qwmtochar(str,true) 
end if 
.......
-----------------------------------------------------------------------------------------------------
3.在线使用
http://www.mytju.com/classcode/tools/quweima.asp
进入以上网址即可在线查阅。