title: C++ : interpreting hex in a char* as uint8_t
body: <p>I need to have a char* in the below format having some hex values.</p>
  
  <pre><code>char *buf = new char(12);
  buf[0] = 0x2;
  buf[1] = 0x0;
  buf[2] = 0x3;
  buf[3] = 0x3;
  buf[4] = 0x0;
  buf[5] = 0x0;
  buf[6] = 0x6;
  buf[7] = 0x4;
  buf[8] = 0x0;
  buf[9] = 0x2;
  buf[10] =0x0;
  buf[11] =0x0;
  </code></pre>
  
  <p>When i try to print them this way -</p>
  
  <pre><code>std::string hex_string;
  create_hex_str((uint8_t*)buf, 12, hex_string);
  std::cout &lt;&lt; hex_string;
  </code></pre>
  
  <p>Definition of the print function :</p>
  
  <pre><code>void create_hex_str(uint8_t *data, int len, std::string &amp;tgt)
  {
      std::stringstream ss;
      ss &lt;&lt; std::hex &lt;&lt; std::setfill('0');
      ss &lt;&lt; "\n";
      for (int i=0; i&lt;len; i++)
      {
          ss &lt;&lt; std::setw(2) &lt;&lt; static_cast&lt;unsigned&gt;(data[i]) &lt;&lt; " ";        
      }
      target = ss.str();
  }
  </code></pre>
  
  <p>I get totally different output :
      f0 a1 56 08 28 02 3c f6 40 f8 56 08</p>
  
  <p>Can somebody please help me out? I am sorry if this is a really trivial question but I can simply not understand what is going on here. What am I doing wrong? And I HAVE to represent the byte array as a char*. </p>