views:

561

answers:

1

So, I have a value of type __be16 (2 bytes). In hex, the value is represented as 0x0800 or 2048 in decimal. (16^2 * 8)

So, when I printf this; I do this:

printf("%04X", value); //__be16 value;
                      //Print a hex value of at least 4 characters, no padding.

output: 0008

printf("%i", value); //Print an integer.

output: 8

I should be getting 0800 and 2048 respectively, what am I doing wrong?

+7  A: 

My guess is that value is 8. :-)

Are you on a little endian machine, such as x86? I'm going to guess that by be16 you mean that the value is big endian and you need to swap the bytes.

asveikau
Swapping byte order can be done like this, by the way: http://www.codeguru.com/forum/showthread.php?t=292902
schnaader
Ah, ntos(value) did the trick.I'm guessing that this wasn't an issue for printing out the MAC addresses (ethernet header) because I was only printing out each MAC 1 short (2 bytes) at a time, and thus avoid endianness altogether?
Lanissum
Good guess! And I too feel I am slowly developing paranormal capabilities from reading SO.
drhirsch