title: Converting chars to byte representation using C++
body: <p>What would be the most efficient yet simplest way to convert a char to its unsigned binary representation (bytes where the MSB is 0)?  I have a method set up like this:</p>
  
  <pre><code>string AsciiToBinary(char value) {
          string binary = "";
          int code = value;
  
          while (code &gt; 0) {
                  if ((code % 2) == 1) {
                          binary.append("1", 1);
                  } else {
                          binary.append("0", 1);
                  }
                  code = code / 2;
          }
          return binary;
  }
  </code></pre>
  
  <p>I am assuming that setting an int to a char sets the char's ASCII value to the int.  However, my results do not match the ASCII table.  I am implementing this function as follows:</p>
  
  <pre><code>char head = stack.pop();
  int code = head; // do not need to parse
  string binary;
  
  while (!stack.isEmpty()) {
          binary = AsciiToBinary(code);
          outfile &lt;&lt; binary &lt;&lt; endl;
          binary.clear();
          head = stack.pop();
          code = head;
  } 
  </code></pre>
  
  <p>I have stored all of the chars in a stack.<br>
    Thank you for info and direction.</p>