title: Print unsigned short int in hex and decimal C
body: <p>I've been trying to print <code>unsigned short int</code> 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.</p>
  <p>Note that I want to print this 16 bits in both a decimal and hexadecimal format, for example <code>00 01</code> as <code>1</code>, another example would be: <code>00 ff</code> as <code>255</code>.</p>
  <p>I have this struct:</p>
  <pre><code>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]; */ 
  };
  </code></pre>
  <p>And this function:</p>
  <pre><code>void print_ARP_msg(struct arphdr arp_hdr) {
     // I need to print the arp_hdr.ar_hrd in both decimal and hex.
     printf(&quot;Format HW: %04x\n&quot;, arp_hdr.ar_hrd);
     printf(&quot;Format Proto: %04x\n&quot;, arp_hdr.ar_pro);
     printf(&quot;HW Len: %x\n&quot;, arp_hdr.ar_hln);
     printf(&quot;Proto Len: %x\n&quot;, arp_hdr.ar_pln);
     printf(&quot;Command: %04x\n&quot;, arp_hdr.ar_op);
  }
  </code></pre>
  <p>The print_ARP_msg function returns me this:</p>
  <blockquote>
  <p>Format HW: 0100</p>
  <p>Format Proto: 0008</p>
  <p>HW Len: 6</p>
  <p>Proto Len: 4</p>
  <p>Command: 0100</p>
  </blockquote>
  <p>The hex values of the struct are &quot;00 01 08 00 06 04 00 01&quot;, so I don't know why it's returning me 0100 in the arp_hdr.ar_hrd value.</p>
  <p>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.</p>
  <p>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 &quot;difference&quot;?</p>