汉字区位码查询

最近有个哥们请我帮他们单位整一个区位码查询的东东。弄了一下,放在这里,请有用的着的哥们参考。有不妥的地方,请大家多提意见。

原理:

汉字的区位码算法:

((汉字的第一个字节-0xa1)*94+(汉字的第二个字节-0xa1))*32

区码=汉字的机器码高字节-A0

位码=汉字的机器码的低字节-A0

汉字文本文件读出的就是汉字的机器吗(C语言或VB都能实现),两个字节

代表一个字的机器吗,高位在前,低位在后

""的区位码为1601,机器吗为B 0A 1(16进制)

c# 版本

public string TextToQwm(string character)

         {

              string coding = "";

              int i1=0,i2=0,i3=0;

              for (int i = 0; i<character.Length; i++ )

              {

                   byte[] bytes = System.Text.Encoding.Default.GetBytes(character.Substring(i,1)); //取出二进制编码内容

                 

                  

                   i1 = (short)(bytes[0] );

                   try

                   {

                       i2 = (short)(bytes[1] );

                       i3=1;

                   }

                   catch(Exception ex)

                   {

                       i2=65536; i3=-1;

                      

                   }

                   finally

                   {

                   int chrasc=i1*256+i2-65536;

                   if (chrasc>0 && chrasc<160)

                   {

                        TextBox2.Text="只能能输入汉字!!";

                   }

                   else

                   {

                       if(i3==-1)

                       {

                            TextBox2.Text="只能能输入汉字!!";

                       }

                       else

                       {

                            string lowCode =System.Convert.ToString(Math.Abs(Convert.ToInt32(System.Convert.ToString(bytes[0]))-160));//取出低字节编码内容(两位16进制)

                          

                            if (lowCode.Length == 1)

                                 lowCode = "0" + lowCode;

                            string hightCode = System.Convert.ToString( Math.Abs(Convert.ToInt32(System.Convert.ToString(bytes[1]))-160));//取出高字节编码内容(两位16进制)

                           

                            if (hightCode.Length == 1)

                                 hightCode = "0" + hightCode;

 

                                 coding +=character.Substring(i,1) + (lowCode + hightCode)    ;// 加入到字符串中,

                       }

                       }

                   }

              }

              return coding;

         }

vbscript 版本

<SCRIPT LANGUAGE="VBScript">
function genqw()
str=f1.txt1.value
newstr=""
for i=1 to len(str)
newstr=newstr&getqw(mid(str,i,1))
next
f1.txt2.value=newstr
end function

function getqw(ch)
casc=asc(ch)
if casc<0 then casc=casc+65535+1
if casc>255 then
b2=right("0"&((casc and 255)-160),2)
b1=right("0"&(int(casc/256)-160),2)
getqw=ch & " "&b1&b2 &" "
else
getqw=ch
end if
end function

</SCRIPT><script language="VBScript" type="text/javascript"> function genqw() str=f1.txt1.value newstr="" for i=1 to len(str) newstr=newstr&getqw(mid(str,i,1)) next f1.txt2.value=newstr end function function getqw(ch) casc=asc(ch) if casc<0 then casc=casc+65535+1 if casc>255 then b2=right("0"&((casc and 255)-160),2) b1=right("0"&(int(casc/256)-160),2) getqw=ch & " "&b1&b2 &" " else getqw=ch end if end function </script><script language="VBScript" type="text/javascript"> function genqw() str=f1.txt1.value newstr="" for i=1 to len(str) newstr=newstr&getqw(mid(str,i,1)) next f1.txt2.value=newstr end function function getqw(ch) casc=asc(ch) if casc<0 then casc=casc+65535+1 if casc>255 then b2=right("0"&((casc and 255)-160),2) b1=right("0"&(int(casc/256)-160),2) getqw=ch & " "&b1&b2 &" " else getqw=ch end if end function </script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值