0

I am converting an 2 bytes long byte array into an short (Int16) and showing it via a messagebox.

MessageBox.Show(BitConverter.ToString(arr) + "\n" + BitConverter.ToInt16(arr, 0));

But the results are a bit strange. Here a few examples:

Array    Short   Correct Result
-------------------------------
00-60    24576            24576
AB-2A    10923            10923
55-55    21845            21845
00-80   -32768            32768
AB-AA   -21845            43691
55-D5   -10923            54613  
1
  • 2
    Why are the results strange? Int16 cannot represent values from 32768 and up, only values from -32768 to +32767. You want UInt16. Commented Jun 21, 2015 at 9:05

1 Answer 1

2

You're using ToInt16, which returns numbers from -32768 to 32767, so numbers out of range will be truncated or adjusted to fit the range. For numbers from 0 through 65535, which seems to be what you want, use ToUInt16 instead:

MessageBox.Show(BitConverter.ToString(arr) + "\n" + BitConverter.ToUInt16(arr, 0));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.