My professor has assigned an encryption algorithm for our homework in C++. Instead of outputting in binary, he'd like the encrypted text (plain text that has run through the cipher) to output as a string in stdout.
The encryption algorithm will typically have an output greater than 128 (which is outside the ASCII range). These are usually replaced with symbols like � or square boxes.
When I go to concatenate these symbols to the output (ciphertext), they sometimes disappear depending on neighboring symbols.
Here's an example:
unsigned char one = 244; // (244 is the 16-bit "output" from the algo)
unsigned char two = 137; // (same as above)
std::string con = "";
con += (one + '\0');
con += (two + '\0');
std::cout << con << std::endl;
The output will be �
, where one of the characters is dropped.
If, however, it was unsigned char one = 244;
and unsigned char two = 244;
, the output in the console will be ��
, so the second char doesn't vanish. I'm not sure why some of these combinations work and others don't. Is there a safer way to concatenate these characters that are outside the normal ASCII range?
I have also tried some things I've found on the site, like:
con += (one + '0');
// but this outputs the wrong text: if it were con += (65 + '0') the
// output is 'q' instead of 'A', but all the symbols generate
// with this
con += (two + '0');
I also tried the following, but it has the same results as the first (missing symbols).
con += one;
con += two;
Thank you!