可以使用BitConverter类中的ToShort方法将两个字节的数组转换回一个短整型值。例如,以下代码将byte数组转换为short类型的值:
byte[] byteArray = { 0x01, 0x02 }; short value = BitConverter.ToInt16(byteArray, 0);
下面是更多的例子:
// Convert byte array to short byte[] byteArray = { 0x01, 0x02 }; short value = BitConverter.ToInt16(byteArray, 0);
// Convert short to byte array short shortValue = -32768; byte[] byteValue = BitConverter.GetBytes(shortValue);
// Convert byte array to short array byte[] byteArray2 = { 0x01, 0x02, 0x03, 0x04 }; short[] shortArray = new short[byteArray2.Length / 2]; for (int i = 0, j = 0; i < byteArray2.Length; i += 2, j++) shortArray[j] = BitConverter.ToInt16(byteArray2, i);