title: C: Using sprintf( ) to do IP address parse, incorrect value
body: <p>I was trying to use <code>sprintf()</code> to parse IP address. <code>buf</code> storing a packet received from other host. And the test data in <code>buf</code> is <code>'\0xff'</code>. I expect to see <code>255.255.255.255</code> in <code>ip</code>. But what I get is <code>-1.-1.-1.-1</code> in <code>ip</code>.</p>
  
  <pre><code>char buf[5];
  char ip[18];
  ...
  // They all have value 0xff, that is buf[0]=buf[1]=buf[2]=buf[3]='\0xff';
  sprintf(ip, "'%d.%d.%d.%d'",buf[0],buf[1],buf[2],buf[3]);
  </code></pre>
  
  <p>It seems in every byte in <code>buf</code> it is added by one. I think it should be something with the specifier, maybe I should use a specifier for <code>unsigned int</code>. Then I tried <code>%u</code>, <code>%hhd</code>. They are not correct. Anyone knows this? Is it because of the specifier?</p>