title: C - converting hex to string
body: <p>Following on an old question <a href="https://stackoverflow.com/questions/25630947/converting-hex-to-string-in-c">Converting hex to string in C?</a></p>
  
  <p>The approved answer suggests to use sprintf to convert each hex to string.</p>
  
  <p>I have two question on this - </p>
  
  <p>1) When i have a hex like <code>0a</code> i want my string to have <code>0a</code> too, but following the above solution the result will have <code>a</code>.</p>
  
  <p>2) What am i doing wrong here?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void)
  {
      unsigned char readingreg[10];
      readingreg[0] = 0x4a;
      readingreg[1] = 0xab;
      readingreg[2] = 0xab;
      readingreg[3] = 0x0a;
      readingreg[4] = 0x40;
      unsigned char temp[10];
      int i = 0;
  
      while (i &lt; 5)
      {
          sprintf(temp + i, "%x", readingreg[i]);
          i++;
      }
      printf("String: %s\n", temp);
      return 0;
  }
  </code></pre>
  
  <p>The o/p seems to - <code>String: 4aaa40</code></p>
  
  <p>3) Combining both the both questions, i want my result string to be <code>4aabab0a40</code></p>
  
  <p>TIA</p>