title: std::cout not properly printing std::string created by reinterpret_cast of unsigned char array
body: <p>I have an <code>unsigned char</code> array that stores 1 byte hex characters and I would like to do bitwise operations on these values.</p>
  
  <p>After using reinterpret_cast to get them into a string (with the idea of using std::stringstream and std::bitset to do the necessary operations), I tried printing the string to see what the contents were. Rather strangely, I noticed that std::cout did not give the expected result, however using printf did! </p>
  
  <p>Here is a quick example:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
  
      unsigned char my_txt[] = {
          0x52, 0x5f, 0x73, 0x68, 0x7e, 0x29, 0x33, 0x74, 0x74, 0x73, 0x72, 0x55
      };
      unsigned int my_txt_len = 12;
  
      std::string my_std_string(reinterpret_cast&lt;const char *&gt;(my_txt), my_txt_len);
  
      for (size_t i=0;i&lt;my_txt_len;i++)
          printf("%02X ", my_std_string[i]);      // Works fine!
      printf("\n");
  
      std::cout &lt;&lt; my_std_string &lt;&lt; std::endl;    // Bad stuff happens :S
  
      return 0;
  }
  </code></pre>
  
  <p>With output:</p>
  
  <pre><code>52 5F 73 68 7E 29 33 74 74 73 72 55     // Expected
  R_sh~)3ttsrU                            // ??
  </code></pre>
  
  <p>Determined to find a solution, I tinkered around for a while - guessing that reinterpret_cast may be causing this behaviour. I eventually found that doing this:</p>
  
  <pre><code>std::cout &lt;&lt; std::hex &lt;&lt; (int)my_std_string[0] &lt;&lt; std::dec &lt;&lt; std::endl;
  </code></pre>
  
  <p>produced the desired result, at least for the first character anyway. Iterating through a loop gave the correct values for the other 11 bytes as well.</p>
  
  <p>Can someone explain why this happens with std::cout and not with printf? At first, I thought that maybe I needed to cast it back to <code>unsigned char</code> but doing this had no effect. Why does casting to <code>int</code> give the correct output?</p>
  
  <p>Is it safe to do bitwise operations on the values stored in a string after using reinterpret_cast as I have used? It has occurred to me that all of this may be rather pointless as I believe I can just do binary math on the unsigned char's directly, no? Advice here would be appreciated.</p>
  
  <p>For those who are curious, I am trying to write a custom C++ console application (Microsoft Visual Studio 2010 on a Windows 7 64-bit machine) to interface with a CAN-USB adapter using the vendor's API. I expect to receive (as part of larger 'receive frame' structs) 8 byte unsigned character arrays with values in hex and I need to process these values to get usable data for my application. This processed data is then being stored in a protocol buffer for further interpretation in matlab.</p>
  
  <p>Sorry if this seems like a silly question - I'm coming from a hardware background and haven't done any serious programming in a while (first post on SO!). </p>