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

作者:萧萧小雨 来源:经典论坛 时间:2009-12-28 10:27:00 

研究编码,得知GB2312编码与区位码的关系,尝试之后,得此程序。

搜索,似乎没人写,故发此地。

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
.......

标签:GB2312,区位码,转换
0
投稿

猜你喜欢

  • J2EE基础应用:J2EE中SQL语句自动构造方法

    2009-09-18 09:06:00
  • python用plotly实现绘制局部放大图

    2021-06-13 06:30:44
  • 图文详解go语言反射实现原理

    2024-02-08 05:01:31
  • localResizeIMG先压缩后使用ajax无刷新上传(移动端)

    2024-05-09 10:35:26
  • thinkphp 多表 事务详解

    2023-07-08 05:43:36
  • javascript增加干扰数据实现简单加密效果

    2011-02-24 11:07:00
  • mysql启用skip-name-resolve模式时出现Warning的处理办法

    2024-01-20 06:44:58
  • Win7 安装软件时无法连接sql server解决方法

    2024-01-18 12:10:59
  • SQLSERVER聚集索引和主键(Primary Key)的误区认识

    2024-01-14 07:49:56
  • python中的一些类型转换函数小结

    2021-11-01 10:42:11
  • 简单上手Python中装饰器的使用

    2023-06-20 19:20:06
  • 如何使用python爬取知乎热榜Top50数据

    2021-11-13 05:47:09
  • python重要函数eval多种用法解析

    2023-02-08 20:16:46
  • python读取Kafka实例

    2023-10-22 17:22:58
  • tkinter禁用(只读)下拉列表Combobox问题

    2021-01-02 13:05:34
  • mysql高级学习之索引的优劣势及规则使用

    2024-01-13 16:21:33
  • 解决linux下使用python打开terminal时报错的问题

    2022-10-06 10:46:25
  • 浅谈python配置与使用OpenCV踩的一些坑

    2022-10-12 07:16:26
  • 一文详解Python中的super 函数

    2022-02-26 03:18:35
  • SQL Server使用T-SQL进阶之公用表表达式(CTE)

    2024-01-17 22:42:32
  • asp之家 网络编程 m.aspxhome.com