汉字区位码查询

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

原理:

////汉字的区位码算法:

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

////

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

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

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

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

////""的区位码为1601,机器吗为B<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="0" unitname="a"></chmetcnv>0A1(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> function genqw() str=f1.txt1.value newstr="" for i=1 to len(str) newstr=newstr&amp;getqw(mid(str,i,1)) next f1.txt2.value=newstr end function function getqw(ch) casc=asc(ch) if casc&lt;0 then casc=casc+65535+1 if casc&gt;255 then b2=right("0"&amp;((casc and 255)-160),2) b1=right("0"&amp;(int(casc/256)-160),2) getqw=ch &amp; " "&amp;b1&amp;b2 &amp;" " else getqw=ch end if end function function genqw() str=f1.txt1.value newstr="" for i=1 to len(str) newstr=newstr&amp;getqw(mid(str,i,1)) next f1.txt2.value=newstr end function function getqw(ch) casc=asc(ch) if casc&lt;0 then casc=casc+65535+1 if casc&gt;255 then b2=right("0"&amp;((casc and 255)-160),2) b1=right("0"&amp;(int(casc/256)-160),2) getqw=ch &amp; " "&amp;b1&amp;b2 &amp;" " else getqw=ch end if end function

你可能感兴趣的:(C++,c,算法,VBScript,vb)