I am trying to write a linux code which will listen on a UDP port and revceive messages from Zyxel modems. (Zyxel modems have a remote packet tracing feature which uses UDP)
I achieved most of the task. The received packet is assigned to a string (char[1024]) variable.
But I need the 15th and 16th bytes of the received data to be converted to integer and assigned to an integer variable. (Because these bytes include the original packet size)

How can I get the 15 & 16th bytes of the char and calculate the integer value ?

With below example I can get the hex values to the string variable x. But that's not what I need. I need to get integer value.
sprintf(x,"%.2x%.2x", buf[15],buf[16]);

Thanks for your help.

9 Years
Discussion Span
Last Post by Salem

I guess you have to take care of the order in which the bytes are packed as well ( e.g/ Big Endian, Little Endian ,etc0


I guess you have to take care of the order in which the bytes are packed as well ( e.g/ Big Endian, Little Endian ,etc0

Not necessary in this case because there is only one byte to the integer.


no; there are 2 bytes. but I don't know the order.
How can I test it ?

int x1 = buf[14]; // 15th byte
int x2 = buf[15]; // 16th byte

what will I do to make up 1 integer which holds the size value I need. ?


Sorry I misunderstood. What I suggested obviously won't work then. Normally I would do this: int x = *(short *)&buf[14]; . But that also assumed a short is two bytes, which may or may not be correct. And it may not work anyway due to byte ordering as previously mentioned.


> but I don't know the order.
Then you'll have to read the documentation to find out. You can't just look at 2 bytes and decide which order they're supposed to be in.

When you have found out, it will be one of

result = ( x1 << 8 ) | x2;
result = x1 | ( x2 << 8 );

>>Then you'll have to read the documentation to find out
There may be other ways too. Lets say we are writing a server program that runs on *nix computer and the clients are a mixture of *nix, MAC and MS-Windows computers. The MS-Windows client program will just have to reverse the bytes of the data coming from the server. But in this case the server will have to look at who sent it the information before it can determine whether to byte swap or not. I think the htons function/macro might resolve that problem, but I'm not all that experienced in network programming.


Thanks to everyone for your answers.

result = ( x1 << 8 ) | x2;
result = x1 | ( x2 << 8 );

What exactly does these commands do ?
when I run it with each of them, I get the results as 20,28,44,32
I was more looking for values over 1000.


This shows the effects of the two calculations Salem posted. The first one assumes the bytes need to be swapped while the second assumes they do not. The buffer that contains the two bytes must be declared as unsigned char otherwise you will get the wrong results.

The code below first copies the binary values of a short int into the unsigned char buffer as it would be done on the sending side of your program. The rest would be executed on the receiving side.

int main()
    short n = 1000;
    unsigned char buf[3];
    // swap bytes
    short result = (buf[0] << 8) | buf[1];
    cout << "result = " << result << "\n";
    // do not swap bytes
    result =  buf[0] | ( buf[1] << 8 );
    cout << "result = " << result << "\n";
    // another way to do it if the bytes to not need to be swapped
    result = *(short *)buf;
    cout << "result = " << result << "\n";

    return 0;


ok; I tried with "unsigned char" but the result is same.
I get values like 32, 20,28 where I was expecting >1000 values.


So create a test case which looks like the one AD posted, and show us your work.

Simply repeating the same old "it doesn't work" is just going nowhere.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.