汉字区位码查询
最近有個哥們請我幫他們單位整一個區位碼查詢的東東。弄了一下,放在這里,請有用的著的哥們參考。有不妥的地方,請大家多提意見。
原理:
漢字的區位碼算法:
((漢字的第一個字節-0xa1)*94+(漢字的第二個字節-0xa1))*32
區碼=漢字的機器碼高字節-A0
位碼=漢字的機器碼的低字節-A0
漢字文本文件讀出的就是漢字的機器嗎(用C語言或VB都能實現),兩個字節
代表一個字的機器嗎,高位在前,低位在后
如"啊"的區位碼為1601,機器嗎為B0A1(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>
總結
- 上一篇: 用于游戏开发和其他目的的光线投射教程
- 下一篇: 开放式基金全景点评:净值全面攀升 关注老