Are you sure each value of your values array fits in an Int16?
If not, then even if you uncheck, the result is not what you want. First you'll have to decide what to do if values[0] or values1 is larger than fits in an Int16.
Your decision depends on what the values mean. Does values[0] represent the Highest 16 bits of your resulting Int32 and values[0] the lowest 16 bits?
In that case you should throw an ArgumentException if either values[0] or values1 is more than Int16.MaxValue. After that your code is easy:
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 could also mean that it is allowed that both values are more than 0x10000, You should think of yourself what you want as a result if the numbers are too big.
By the way, if your values each represent one half of an Int32, consider switching the meaning of values[0] and values1. Almost always the least significant bits (LSB) are in [0], while the most significant bits (MSB) are in 1. If you follow this convention, you don't have 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);
uncheck
to get around it. – ClaudelistByte.AddRange(BitConverter.GetBytes(shortArr[0])); listByte.AddRange(BitConverter.GetBytes(shortArr[1])); Int32 result = BitConverter.ToInt32(listByte.ToArray());
– Irradiance