Are you sure that each value in your array of values ββcorresponds to Int16?
If not, then even if you uncheck the box, the result is not the one you want. First you will need to decide what to do if the values ββ[0] or the value 1 is greater than what is suitable for Int16.
Your decision depends on what the meanings mean. Are the values ββ[0] representing the highest 16 bits of your resulting Int32 and the values ββ[0] the lower 16 bits?
In this case, you should throw an ArgumentException if [0] or 1 is greater than Int16.MaxValue. After that, your code is simple:
if (values[0] > Int16.MaxValue || values[1] > Int16.MaxValue) throw new ArgumentException("values should fit in two bytes"); Int32 result = values[0] << 0x10000 + values[1]; return result;
It can also mean that it is allowed that both values ββare greater than 0x10000, you should think about yourself what you want, as a result, if the numbers are too large.
By the way, if your values ββrepresent one half of Int32, consider switching the values ββof [0] and 1 . Almost always, the least significant bits (LSB) are at [0], and the most significant bits (MSB) are at 1 . If you follow this convention, you do not need to write these converters yourself, you can use the BitConverter class
Int32 original = 0x12345678; byte[] bytes = BitConverter.GetBytes(original); foreach (var b in bytes) Console.WriteLine(b.ToString("02X"); // write in two digit hex format // will write lines with 78 56 34 12 Int16[] Values = new Int16[] { BitConverter.ToInt16(bytes), // LSB BitConverter.ToInt16(bytes, 2), // MSB }; Int32 result = (Int32)values[0] + (Int32)values[1] << 0x10000; Debug.Assert(original == result);
source share