Simple question here. Having a problem with the XOR operator (^). I can XOR integers fine and get the correct results; however, when XORing two chars, nothing is returned:
int main() {
char a = 'a';
char b = 'b';
char c;
c = a^b;
cout << c << endl;
}
Nothing happens. No output. What am I doing wrong here. I’m trying to use this to XOR encrypt a string with a given key, but am having issues with this operation. I guess I could get the ASCII value and convert it to its corresponding char, but this tool will be used to encrypt files as well as plain text so I would like to avoid that at all costs.
std::ostream::operator<<(andstd::coutis anstd::ostream) outputs formatted text. When you pass it achar, it’s trying to format and output a character (think ASCII). If thecharisn’t a printable character, it doesn’t print it out (for obvious reasons). If you want it to output a formated integer, you need to cast it:For what it’s worth, if you use the unformatted output function
std::ostream::write, it will output unformatted characters. Beware, however, because if you try to write out an unprintable character, it will “work,” but your console will not show it (because again, it’s an unprintable character).