Reputation: 6451
I have byte array
byte[] PixelData = {255,235};
I want to convert it to short and before that I want to get the first 13 bit before the conversion because when I convert using the following code improper values return
short val1 = 0;
val1 = BitConverter.ToInt16(PixelData, 0);
any ideas how to do that
Upvotes: 1
Views: 434
Reputation: 1063704
Most likely this is an endianness issue. If unsure, use shifting instead:
short val1 = (short) ((PixelData[0] << 8) | (PixelData[1]));
if you really need the 13 bits, use an &
mask:
short val1 = (short) (((PixelData[0] << 8) | (PixelData[1])) & 8191);
Note: I've assumed big-endian in the above; if your data is little-endian, just reverse them:
short val1 = (short) ((PixelData[0]) | (PixelData[1] << 8));
and
short val1 = (short) (((PixelData[0]) | (PixelData[1] << 8)) & 8191);
Upvotes: 4