Convert 16-bit signed int to 2 bytes?

Hi, I have a simple problem that has been messing with me, and I can find a solution for it. I got an array that contains signed int data, I need to convert each value into an array of 2 bytes. I am using C # and I tried using BitConverter.GetBytes (int), but it returns a 4 byte array.

Any help?

thanks tristan

+7
c #
source share
1 answer

A signed 16-bit value is best represented as short rather than int - so use BitConverter.GetBytes(short) .

However, as an alternative:

 byte lowByte = (byte) (value & 0xff); byte highByte = (byte) ((value >> 8) & 0xff); 
+15
source share

All Articles