title: Encode std::string with hex values
body: <p>My application requires some hex values to be encoded and transmitted in std::string. So I'm doing like this.</p>
  
  <pre><code>static string printHex(const string&amp; str)
  {
      stringstream ss;
      ss &lt;&lt; "[ " &lt;&lt; hex;
      for (int i = 0; i &lt; str.size(); i++)
      {
          ss &lt;&lt; (((uint32_t)str[i] )&amp; 0xFF) &lt;&lt; " ";
      }
      ss &lt;&lt; "]" &lt;&lt; dec;
  
      return ss.str();
  }
  
  int main()
  {
      char ptr[] = {0xff, 0x00, 0x4d, 0xff, 0xdd};// &lt;--see here, 0x00 is the issue.
      string str(ptr);
      cout &lt;&lt; printHex(str) &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>Obviously the string is taking values only upto 0x00, the rest of the data is lost. Without 0x00 it'll work for any values. But I need 0x00 also. Please suggest a solution. Thanks for the help.</p>