Convert 16-bit signed int to 2-bytes?
I got an array which contains signed int data, i need to convert each value in the array to 2 bytes. I am using C# and i tried using BitCo开发者_JAVA技巧nverter.GetBytes(int)
but it returns a 4 byte array.
A signed 16-bit value is best represented as a short
rather than int
- so use BitConverter.GetBytes(short)
.
However, as an alternative:
byte lowByte = (byte) (value & 0xff);
byte highByte = (byte) ((value >> 8) & 0xff);
精彩评论