I receive a port number as 2 bytes (least significant byte first) and I want to convert it into an integer so that I can work with it. I\'ve made this:
char buf[
char buf[2]; //Where the received bytes are
int number;
number = *((int*)&buf[0]);
&buf[0]
takes address of first byte in buf.
(int*)
converts it to integer pointer.
Leftmost *
reads integer from that memory address.
If you need to swap endianness:
char buf[2]; //Where the received bytes are
int number;
*((char*)&number) = buf[1];
*((char*)&number+1) = buf[0];