title: character string to bitset in C++
body: <p>I am still kind of new to C++ and am trying to figure out what I am not able to pass a value correctly to a bitset, at least I suspect that is what the problem is. 
  I wrote a small function to assist in flipping the bits of a hex value to reverse the endian. So example would be input 0x01 and it would return 0x80. </p>
  
  <p>This is the code I wrote.</p>
  
  <pre><code>int flipBits(char msd, char lsd) {
  
  char ch[5];
  sprintf_s(ch, "0x%d%d", msd, lsd);
  char buffer[5];
  strncpy_s(buffer, ch, 4);
  cout &lt;&lt; ch &lt;&lt; endl;
  cout &lt;&lt; buffer &lt;&lt; endl;
  bitset&lt;8&gt; x(buffer);
  bitset&lt;8&gt; y;
  
  for (int i = 0; i &lt; 8; i++) {
      y[i] = x[7 - i];
  }
  
  cout &lt;&lt; y &lt;&lt; endl;              // print the reversed bit order
  int b = y.to_ulong();           // convert the binary to int
  
  cout &lt;&lt; b &lt;&lt; endl;              // print the int
  cout &lt;&lt; hex &lt;&lt; b &lt;&lt; endl;       // print the hex
  
  
  return b;
  }
  </code></pre>
  
  <p>I tried adding the strncpy because I thought maybe the null terminator from sprintf was not working properly with the bitset. If in the line </p>
  
  <pre><code>bitset&lt;8&gt; x(buffer);
  </code></pre>
  
  <p>I replace buffer with a hex value, say for example 0x01, then it works and prints out 0x80 as I would expect, but if I try to pass in the value with the buffer it doesn't work. </p>