title: how to print 2 or 4 bytes in hex format in C
body: <p>I need to print bytes in <code>hexadecimal</code> form in <code>C</code>. All I’ve managed to do is to print 1 byte at a time, where my program has to support the option for 1, 2 or 4 bytes (size parameter).
  this is my function:</p>
  
  <pre><code>void memDisaply(char* filename, int size) {
  
  char sentence [100];
  unsigned int str[20];
  int length,i;
  unsigned char* ptr;
  
  printf("Please enter &lt;address&gt; &lt;length&gt;\n");
  
  fflush(stdin);
  fgets(sentence,100,stdin);
  fgets(sentence,100,stdin);
  
      sscanf (sentence,"%x %d",str,&amp;length);
      ptr = (unsigned char*)str;
  
      for (i=0 ; i&lt; length ; i++) {
          printf ("%02x",(unsigned int)ptr[i]);
      }
  
      /*reset values*/
      length = 0;
      i = 0;
      ptr = NULL;
  }
  </code></pre>
  
  <p>the output is as follows (where size=1, and length= 10):</p>
  
  <pre><code>31 ED 5E 89 E1 83 E4 F0 50 54
  </code></pre>
  
  <p>I need that when <code>size = 2</code> and <code>length = 5</code> that the output will be:</p>
  
  <pre><code>ED31 895E 83E1 F0E4 5450
  </code></pre>
  
  <p>(notice the <code>little endian</code>)
  I somehow need to use the size argument to alter the output but I don't know how.
  I assume it got something to do with the <code>%02x</code> format, and I've tried <code>%04x</code> but all I got was:</p>
  
  <pre><code>0031 00ED 005E 0089 00E1 0083 00E4 00F0 0050 0054
  </code></pre>