汉字区位码查询
最近有個(gè)哥們請(qǐng)我?guī)退麄儐挝徽粋€(gè)區(qū)位碼查詢的東東。弄了一下,放在這里,請(qǐng)有用的著的哥們參考。有不妥的地方,請(qǐng)大家多提意見。
原理:
漢字的區(qū)位碼算法:
((漢字的第一個(gè)字節(jié)-0xa1)*94+(漢字的第二個(gè)字節(jié)-0xa1))*32
區(qū)碼=漢字的機(jī)器碼高字節(jié)-A0
位碼=漢字的機(jī)器碼的低字節(jié)-A0
漢字文本文件讀出的就是漢字的機(jī)器嗎(用C語(yǔ)言或VB都能實(shí)現(xiàn)),兩個(gè)字節(jié)
代表一個(gè)字的機(jī)器嗎,高位在前,低位在后
如"啊"的區(qū)位碼為1601,機(jī)器嗎為B0A1(16進(jìn)制)
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)); //取出二進(jìn)制編碼內(nèi)容
????????????? ???
??????????????????
?????????????????? 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));//取出低字節(jié)編碼內(nèi)容(兩位16進(jìn)制)
??????????????????????????
??????????????????????????? if (lowCode.Length == 1)
???????????????????????????????? lowCode = "0" + lowCode;
??????????????????????????? string hightCode = System.Convert.ToString( Math.Abs(Convert.ToInt32(System.Convert.ToString(bytes[1]))-160));//取出高字節(jié)編碼內(nèi)容(兩位16進(jìn)制)
???????????????????????????
??????????????????????????? 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>
總結(jié)
- 上一篇: 用于游戏开发和其他目的的光线投射教程
- 下一篇: 开放式基金全景点评:净值全面攀升 关注老