title: conversion of uint8_t to a string [C]
body: <p>I'm trying to "translate" an array of uint8_t [uint8_t lets_try[16]] to a string of 16*8+1[null character] elements. For example:</p>
  
  <pre><code>lets_try[0] = 10101010  
  lets_try[1] = 01010101  
  </code></pre>
  
  <p>...</p>
  
  <p>and I would like to have a string like:</p>
  
  <p>1010101001010101...[\0]</p>
  
  <p>Here the questions: 1) is there a quick way to perform this operation?</p>
  
  <p>I was trying to do it on my own; my idea was starting from translating a single uint8_t variable into a string and obtaining the full array with a loop [I haven't done this last part yet]. At the end I wrote this code:</p>
  
  <pre><code>int main()
  {
      uint8_t example = 0x14;
      uint8_t *pointer;
      char *final_string;
  
      pointer = &amp;example;
  
      final_string = convert(pointer);
      puts(final_string);
  
      return(0);
  }
  
  
  char *convert (uint8_t *a)
  {
      int buffer1[9];
      char buffer2[9];
      int i;
      char *buffer_pointer;
  
      buffer1[8]='\0';
  
      for(i=0; i&lt;=7; i++)
          buffer1[7-i]=( ((*a)&gt;&gt;i)&amp;(0x01) );
  
      for(i=0; i&lt;=7; i++)
          buffer2[i] = buffer1[i] + '0';
  
      buffer2[8] = '\0';
  
      puts(buffer2);
  
      buffer_pointer = buffer2;
  
      return buffer_pointer;
  }
  </code></pre>
  
  <p>Here other few questions:</p>
  
  <p>2) I'm not sure I fully understand the magic in this expression I found online:
  buffer2[i] = buffer1[i] + '0'; can somebody explain to me why the following puts(buffer2) is not going to work correctly without the +'0'? is it the null character at the end of the newborn string which makes the puts() work? [because with the null character it knows it's printing a real string?]</p>
  
  <p>3) in the code above puts(buffer2) gives the right output while the puts in main() gives nothing; I'm going mad in looking again and again the code, I can't find what's wrong with that</p>
  
  <p>4) in my solution I manage to convert an uint8_t into a string passing from an array of int:
  uint8_t->int array->string; is there a way to shorten this procedure, passing directly from the uint8_t into a string, or improve it? [in forums I found only solutions in C++] it works but I find it a little heavy and not so elegant</p>
  
  <p>Thanks everybody for the support</p>