应用场景:接收到LIN报文有8个细节数据,表示为:
byte[] data={0x27,0x23,0x34,0x24,0x45,0x32,0xFF,0xFF};希望得到的解析后数据为:value1=2.317;value2=2.434;value3=3.245;即第1和2两个字节组成value1,第2和4两个字节组成value2,第5和6两个字节组成value3,第7和第8个字节不参与。
实现代码如下:
string[] temp = new string[6]; byte[] Data = { 0x27, 0x23, 0x34, 0x24, 0x45, 0x32,0xFF,0xFF}; temp[0] = string.Format("{0:X}", Data[0]); temp[1] = string.Format("{0:X}", Data[1]); temp[2] = string.Format("{0:X}", Data[2]); temp[3] = string.Format("{0:X}", Data[3]); temp[4] = string.Format("{0:X}", Data[4]); temp[5] = string.Format("{0:X}", Data[5]); TextBox1.Text= (double.Parse(temp[0]) * 0.001 + double.Parse(temp[1]) * 0.1).ToString(); TextBox2.Text = (double.Parse(temp[2]) * 0.001 + double.Parse(temp[3]) * 0.1).ToString(); TextBox3.Text = (double.Parse(temp[4]) * 0.001 + double.Parse(temp[5]) * 0.1).ToString();