如何把一个字符串数组赋值给一个BYTE类型的数组,例如:
string [] SendData = new string[2];
SendData[0] = "68";
SendData[1] = "69";我希望得到的是:
byte [] send=new byte [2];
send[0]=0X68,
send[1]=0X69,
也就是说:send[]这个数组和 SendData[]这个数组比较,不同的是类型不一样,而且前面多了0X这2个字符,请问用程序如何实现?
string [] SendData = new string[2];
SendData[0] = "68";
SendData[1] = "69";我希望得到的是:
byte [] send=new byte [2];
send[0]=0X68,
send[1]=0X69,
也就是说:send[]这个数组和 SendData[]这个数组比较,不同的是类型不一样,而且前面多了0X这2个字符,请问用程序如何实现?
{
System.Collections.Generic.List<byte> AList = new System.Collections.Generic.List<byte>();
foreach(string str in AData)
{
AList.Add(byte.Parse(str,System.Globalization.NumberStyles.HexNumber));
}
return AList.ToArray();
}
{
byte[] send = new byte[SendData.Length];
for (int i = 0; i < SendData.Length; i++)
send[i] = Convert.ToByte(SendData[i], 16);
return send;
}
SendData[0] = "68";
SendData[1] = "69";
byte[] send = new byte[2];
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < SendData.Length; j++)
{
send[j] = byte .Parse( SendData[j]);
Console.WriteLine("\\0x"+send[j]);
}
}
send[i] = byte.Parse( SendData[i]);
2个ASCII码可以放在有2个元素的字节数组中
2个汉字的Unicode码需要放在4个元素的字节数组中使用Encoding.Ascii.Getbyte方法试试
SendData[0] = "68";
SendData[1] = "69"; byte[] send = new byte[2]; send[0] = byte.Parse( SendData[0],System.Globalization.NumberStyles.HexNumber);
send[1] = byte.Parse( SendData[1],System.Globalization.NumberStyles.HexNumber);
0X是表示十六进制,和实际数据没关系,显示的时候需要的话可以加上,不过不会存储到send里的