title: Use printf to print character string in hexadecimal format, distorted results
body: <p>I want to print a character string in hexadecimal format on machine A. Something like:</p>
  <pre><code>ori_mesg = gen_rdm_bytestream (1400, seed)
  sendto(machine B, ori_mesg, len(mesg))
  </code></pre>
  <p>On machine B</p>
  <pre><code>recvfrom(machine A, mesg)
  
  mesg_check = gen_rdm_bytestream (1400, seed)
  
  for(i=0;i&lt;20;i++){
      printf(&quot;%02x &quot;, *(mesg+i)&amp; 0xFF);
  }
  printf(&quot;\n&quot;);
  for(i=0; i&lt;20; i++){
      printf(&quot;%02x &quot;, *(mesg_check+i));
  }
  printf(&quot;\n&quot;);
  </code></pre>
  <p><code>seed</code> varies among 1, 2, 3, ...</p>
  <p>The bytes generation funcion is:</p>
  <pre><code>u_char *gen_rdm_bytestream (size_t num_bytes, unsigned int seed)
  {
      u_char *stream = malloc (num_bytes+4);
      size_t i;
  
      u_int16_t seq = seed;
      seq = htons(seq);
      u_int16_t tail = num_bytes;
      tail = htons(tail);
      memcpy(stream, &amp;seq, sizeof(seq));
      srand(seed);
      for (i = 3; i &lt; num_bytes+2; i++){
          stream[i] = rand ();
      }
      memcpy(stream+num_bytes+2, &amp;tail, sizeof(tail));
  
      return stream;
  }
  </code></pre>
  <p>But I got results from printf like:</p>
  <pre><code>00 01 00 67 c6 69 73 51 ff 4a ec 29 cd ba ab f2 fb e3 46 7c
  00 01 00 67 ffffffc6 69 73 51 ffffffff 4a ffffffec 29 ffffffcd ffffffba ffffffab fffffff2 fffffffb ffffffe3 46 7c
  </code></pre>
  <p>or</p>
  <pre><code>00 02 88 fa 7f 44 4f d5 d2 00 2d 29 4b 96 c3 4d c5 7d 29 7e
  00 02 00 fffffffa 7f 44 4f ffffffd5 ffffffd2 00 2d 29 4b ffffff96 ffffffc3 4d ffffffc5 7d 29 7e
  </code></pre>
  <p>Why are there so many <code>fffff</code> for <code>mesg_check</code>?</p>
  <p>Are there any potential reasons for this phenomenon?</p>