I have an unsigned char array with 2 elements that represents a signed integer. How can I convert these 2 bytes into a signed integer?
Edit: The unsigne
For maximum safety, use
int i = *(signed char *)(&c[0]); i *= 1 << CHAR_BIT; i |= c[1];
for big endian. Swap c[0] and c[1] for little endian.
c[0]
c[1]
(Explanation: we interpret the byte at c[0] as a signed char, then arithmetically left shift it in a portable way, then add in c[1].)
signed char