1

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:

string AsciiToBinary(char value) {
        string binary = "";
        int code = value;

        while (code > 0) {
                if ((code % 2) == 1) {
                        binary.append("1", 1);
                } else {
                        binary.append("0", 1);
                }
                code = code / 2;
        }
        return binary;
}

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:

char head = stack.pop();
int code = head; // do not need to parse
string binary;

while (!stack.isEmpty()) {
        binary = AsciiToBinary(code);
        outfile << binary << endl;
        binary.clear();
        head = stack.pop();
        code = head;
} 

I have stored all of the chars in a stack.
Thank you for info and direction.

bama_programmer
  • 185
  • 1
  • 1
  • 11
  • no - you have stored all the `string`'s on the stack –  Nov 09 '15 at 21:14
  • What is this "binary C++" you mention in the title? FYI: Tags don't belong in the title! That said, please read all the guidelines, you are supposed to provide a minimal but complete example and some other info. As it stands, your question is off-topic and there are good reasons for that. – Ulrich Eckhardt Nov 09 '15 at 21:37
  • Here is the populated stack: [ d g q q a p l j ] I am trying to convert each char letter to an unsigned byte. For 'd', my program is outputting '1010', which is not correct. – bama_programmer Nov 09 '15 at 21:52
  • Possible duplicate of [Changing integer to binary string of digits](http://stackoverflow.com/questions/8222127/changing-integer-to-binary-string-of-digits) – Snowhawk Nov 09 '15 at 22:04

2 Answers2

0

std::string::append() adds the character onto the end of the string. So you are putting the bits on in the reverse order: the LSB is the first character and vice versa. Try this: binary.insert (0, 1, (code % 2 == 1) ? '1' : '0');

  • very clever, thank you very much willywonka_dailyblah! – bama_programmer Nov 09 '15 at 21:31
  • @bama_programmer np bro. one more tip: instead of `code % 2` use `code & 1` instead - saves a division :) –  Nov 09 '15 at 21:32
  • I'm finally outputting bytes like I want, but they are not the correct unsigned bytes as seen in the ASCII table. I may end up hashing each char with its corresponding unsigned byte since I'm just messing around. I am just sort of ticked off that I can't do it the straight forward way. – bama_programmer Nov 09 '15 at 22:35
  • @bama_programmer I think i might know the reason: cast the `char` to an `unsigned char` first, *then* cast to `int` –  Nov 09 '15 at 22:51
  • @bama_programmer ho ho ho –  Nov 10 '15 at 09:30
0

This method works well and is editable for all those interested and learning C++:

using namespace std; // bad for updates
#include <string>

string AsciiToBinary(char value) {
        string binary = "";
        unsigned int code = value;
        unsigned int chk = value;

        while (code > 0) {
                if ((code & 1) == 1) {
                        binary.append("1", 1);
                } else {
                        binary.append("0", 1);
                }
                code = code / 2;
        }
        reverse(binary.begin(), binary.end());

        if (chk < 64) {
                binary.insert(0, "00");
        } else {
                binary.insert(0, "0");
        }

        return binary;
}
bama_programmer
  • 185
  • 1
  • 1
  • 11