主题:请大家帮帮忙,关系到我能否保住工作的问题!~~~
老总要我写一个软件,基本的功能都实现了,现在只有最后一个问题,就是如何把汉字转换成区位码,我在网上找了很多很多的贴子,也看了很多的贴子,可是版本只有两个,一个是C#版的,一个是VBScrip版的,我采用C#版的,程序代码如下:
textBox1.MaxLength = 8;
string st = textBox1.Text;
int h;
int l;
for (int i = 0; i < st.Length; i++)
{
byte[] bb = new byte[8];
bb[0] = Convert.ToByte(0xf7);
bb[1] = Convert.ToByte(numericUpDown1.Value / 256);
bb[2] = Convert.ToByte(numericUpDown1.Value % 256);
bb[3] = Convert.ToByte(0x68);
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(st.Substring(i, 1)); //取出二进制编码内容
string lowCode = System.Convert.ToString(bytes[0], 16); //取出低字节编码内容(两位16进制)
if (lowCode.Length == 1)
lowCode = lowCode.PadLeft(1, '0');//如果低字节不足两位时,添加0补足
l = Convert.ToInt32(lowCode);
string hightCode = System.Convert.ToString(bytes[1], 16);//取出高字节编码内容(两位16进制)
if (hightCode.Length == 1)
hightCode = hightCode.PadLeft(1, '0');
h = Convert.ToInt32(hightCode);
bb[4] = Convert.ToByte(h);
bb[5] = Convert.ToByte(l);
bb[6] = 00;
bb[7] = (byte)(bb[1] + bb[2] + bb[3] + bb[4] + bb[5] + bb[6]);
if (axMSComm2.PortOpen == true)
{
axMSComm2.Output = bb;
}
}
}
我把“我”字用这个程序转换成区位码后是3E0B(十六进制),而不是2132(十六进制)???
请大家帮帮忙,不然的话,我会被炒!~~~~
textBox1.MaxLength = 8;
string st = textBox1.Text;
int h;
int l;
for (int i = 0; i < st.Length; i++)
{
byte[] bb = new byte[8];
bb[0] = Convert.ToByte(0xf7);
bb[1] = Convert.ToByte(numericUpDown1.Value / 256);
bb[2] = Convert.ToByte(numericUpDown1.Value % 256);
bb[3] = Convert.ToByte(0x68);
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(st.Substring(i, 1)); //取出二进制编码内容
string lowCode = System.Convert.ToString(bytes[0], 16); //取出低字节编码内容(两位16进制)
if (lowCode.Length == 1)
lowCode = lowCode.PadLeft(1, '0');//如果低字节不足两位时,添加0补足
l = Convert.ToInt32(lowCode);
string hightCode = System.Convert.ToString(bytes[1], 16);//取出高字节编码内容(两位16进制)
if (hightCode.Length == 1)
hightCode = hightCode.PadLeft(1, '0');
h = Convert.ToInt32(hightCode);
bb[4] = Convert.ToByte(h);
bb[5] = Convert.ToByte(l);
bb[6] = 00;
bb[7] = (byte)(bb[1] + bb[2] + bb[3] + bb[4] + bb[5] + bb[6]);
if (axMSComm2.PortOpen == true)
{
axMSComm2.Output = bb;
}
}
}
我把“我”字用这个程序转换成区位码后是3E0B(十六进制),而不是2132(十六进制)???
请大家帮帮忙,不然的话,我会被炒!~~~~