tags:

views:

232

answers:

1

Hi i got a simple problem that has been bugging me and i can find a solution to it. 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 BitConverter.GetBytes(int) but it returns a 4 byte array.

Any help?

thanks tristan

+6  A: 

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);
Jon Skeet