Print unsigned short int in hex and decimal C

Viewed 8588

I've been trying to print unsigned short int values in C with no luck. As far as I know, it's a 16 bit value, so I've tried several different methods to print this 2 bytes together, but I've only been able to print it correctly when doing it byte by byte.

Note that I want to print this 16 bits in both a decimal and hexadecimal format, for example 00 01 as 1, another example would be: 00 ff as 255.

I have this struct:

struct arphdr {
   unsigned short int ar_hrd;
   unsigned short int ar_pro;
   unsigned char ar_hln;
   unsigned char ar_pln;
   unsigned short int ar_op;
   // I'm commenting the following part because I won't need it now
   // for the explanation
   /* unsigned char __ar_sha[ETH_ALEN];
   unsigned char __ar_sip[4];
   unsigned char __ar_tha[ETH_ALEN];
   unsigned char __ar_tip[4]; */ 
};

And this function:

void print_ARP_msg(struct arphdr arp_hdr) {
   // I need to print the arp_hdr.ar_hrd in both decimal and hex.
   printf("Format HW: %04x\n", arp_hdr.ar_hrd);
   printf("Format Proto: %04x\n", arp_hdr.ar_pro);
   printf("HW Len: %x\n", arp_hdr.ar_hln);
   printf("Proto Len: %x\n", arp_hdr.ar_pln);
   printf("Command: %04x\n", arp_hdr.ar_op);
}

The print_ARP_msg function returns me this:

Format HW: 0100

Format Proto: 0008

HW Len: 6

Proto Len: 4

Command: 0100

The hex values of the struct are "00 01 08 00 06 04 00 01", so I don't know why it's returning me 0100 in the arp_hdr.ar_hrd value.

Also, I made a function which prints the struct in hex, to make sure that I'm doing it right, and I was able to check, that all the fields where correctly assigned.

PS: before sending this question, I realized that it's printing the correct Hex values but disordered. Could it be related to the little/big endian "difference"?

3 Answers
Related