25

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[2]; //Where the received bytes are

char port[2];

port[0]=buf[1]; 

port[1]=buf[0];

int number=0;

number = (*((int *)port));

However, there's something wrong because I don't get the correct port number. Any ideas?

3
  • is your endianness the same? Commented Jun 12, 2013 at 17:17
  • 1
    also 2 bytes vs 4 bytes: short vs int Commented Jun 12, 2013 at 17:18
  • 1
    use uint16_t to do the cast Commented Jun 12, 2013 at 17:18

5 Answers 5

37

I receive a port number as 2 bytes (least significant byte first)

You can then do this:

  int number = buf[0] | buf[1] << 8;
3
  • 5
    @user1367988 Just beware in case char is signed on that platform. Commented Jun 12, 2013 at 18:28
  • @AnttiHaapala--СлаваУкраїні How is this incorrect? It looks equivalent to Joachim's.
    – kwc
    Commented Jul 30, 2022 at 0:26
  • This answer is wrong and will break badly on x86 if either of the bytes has sign bit set, unless you use unsigned char for the buffer. @kwc joachim's says: "if you make it unsigned char". I remove the comment because whereas Joachim's answer is more correct it is still not 100 % portable - for 16 bit ints it invkes undefined behaviour. But this answer is very much broken on x86 and signed chars, unless the type of the buffer is changed. Since both are converted to signed, if either byte is Commented Jul 30, 2022 at 6:09
15

If you make buf into an unsigned char buf[2];, you can simplify it to:

number = (buf[1] << 8) + buf[0];
7

I appreciate this has already been answered reasonably. However, another technique is to define a macro in your code eg:

// bytes_to_int_example.cpp
// Output: port = 514

// I am assuming that the bytes the bytes need to be treated as 0-255 and combined MSB -> LSB

// This creates a macro in your code that does the conversion and can be tweaked as necessary
#define bytes_to_u16(MSB,LSB) (((unsigned int) ((unsigned char) MSB)) & 255)<<8 | (((unsigned char) LSB)&255) 
// Note: #define statements do not typically have semi-colons
#include <stdio.h>

int main()
{
  char buf[2];
  // Fill buf with example numbers
  buf[0]=2; // (Least significant byte)
  buf[1]=2; // (Most significant byte)
  // If endian is other way around swap bytes!

  unsigned int port=bytes_to_u16(buf[1],buf[0]);

  printf("port = %u \n",port);

  return 0;
}
0

Least significant byte: int number = (uint8_t)buf[0] | (uint8_t)buf[1] << 8;

Most significant byte: int number = (uint8_t)buf[1] << 8 | (uint8_t)buf[0];

-2
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];
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.