How do I merge two unsigned chars into a single unsigned short in c++. The Most Significant Byte in the array is contained in array[0] and the Least Significant Byte is located at array[1] . (Big endian)
How to merge two unsigned chars into a single unsigned short(16 bits) in c++
734 views Asked by thecoder At
2
(array[0] << 8) | array[1]Note that an
unsigned charis implicitly converted ("promoted") tointwhen you use it for any calculations. Thereforearray[0] << 8doesn't overflow. Also the result of this calculation is anint, so you may cast it back to unsigned short if your compiler issues a warning.