0

I have a simple program and I'm trying to write a sequence of hex values of byte patterns to a text file in the following format:

arbitrary data representation

3f 42 5c b8 d9 0a // etc... 

Here is my program.

int main() { char digits[] = { 0x7e, 0x30, 0x6d, 0x79, 0x33, 0x5b, 0x5f, 0x70, 0x7f, 0x7b }; std::fstream file; file.open("display.txt", std::ios::out); // Ones place for (int val = 0; val <= 255; val += 1) file << std::hex << digits[val % 10] << " "; file << std::endl; // Tens place for (int val = 0; val <= 255; val += 1) file << std::hex << digits[(val / 10) % 10] << " "; file << std::endl; // Hundreds place for (int val = 0; val <= 255; val += 1) file << std::hex << digits[(val / 100) % 10] << " "; file << std::endl; // Sign digit for (int val = 0; val <= 255; val += 1) file << std::hex << 0 << " "; file << std::endl; // Ones Place Twos Complement for (int val = -128; val <= 127; val += 1) file << std::hex << digits[abs(val) % 10] << " "; file << std::endl; // Tens Place Twos Complement for (int val = -128; val <= 127; val += 1) file << std::hex << digits[abs(val / 10) % 10] << " "; file << std::endl; // Hundreds Place Twos Complement for (int val = -128; val <= 127; val += 1) file << std::hex << digits[abs(val / 100) % 10] << " "; file << std::endl; // Sign Digit Twos Complement for (int val = -128; val <= 127; val += 1) { if (val < 0) file << std::hex << 01; else { file << std::hex << 00; } file << " "; } file << std::endl; file.close(); return 0; } 

However, after I run the program and open my text file, it's showing up as if it's in binary... I'm not sure about what I'm doing wrong, what I'm missing, or overlooking.

Here's what my text file looks like:

⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛਍⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾‰‰‰‰‰‰‰‰‰‰⁹⁹⁹⁹⁹⁹⁹⁹⁹⁹″″″″″″″″″″⁛⁛⁛⁛⁛⁛⁛⁛⁛⁛          ⁰⁰⁰⁰⁰⁰⁰⁰⁰⁰ⁿⁿⁿⁿⁿⁿⁿⁿⁿⁿ⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾‰‰‰‰‰‰‰‰‰‰⁹⁹⁹⁹⁹⁹⁹⁹⁹⁹″″″″″″″″″″⁛⁛⁛⁛⁛⁛⁛⁛⁛⁛          ⁰⁰⁰⁰⁰⁰⁰⁰⁰⁰ⁿⁿⁿⁿⁿⁿⁿⁿⁿⁿ⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾‰‰‰‰‰‰‰‰‰‰⁹⁹⁹⁹⁹⁹⁹⁹⁹⁹″″″″″″″″″″⁛⁛⁛⁛⁛⁛਍⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰਍‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰਍ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾⁻ⁿ⁰ ⁛″⁹‰⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰ⁿ⁻⁾‰⁹″⁛ ⁰਍‰‰‰‰‰‰‰‰‰‰⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻ⁿⁿⁿⁿⁿⁿⁿⁿⁿⁿ⁰⁰⁰⁰⁰⁰⁰⁰⁰⁰          ⁛⁛⁛⁛⁛⁛⁛⁛⁛⁛″″″″″″″″″″⁹⁹⁹⁹⁹⁹⁹⁹⁹⁹‰‰‰‰‰‰‰‰‰‰⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾‰‰‰‰‰‰‰‰‰‰⁹⁹⁹⁹⁹⁹⁹⁹⁹⁹″″″″″″″″″″⁛⁛⁛⁛⁛⁛⁛⁛⁛⁛          ⁰⁰⁰⁰⁰⁰⁰⁰⁰⁰ⁿⁿⁿⁿⁿⁿⁿⁿⁿⁿ⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾‰‰‰‰‰‰‰‰‰‰਍‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾⁾‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰਍‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‱‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰‰਍ 

It's not even printing new lines from std::endl.

EDIT

I've been doing some testing; and I'm suspecting that it has to do with the array's type in combination with std::hex...

I'm getting this binary dump when using char, unsigned char, std::uint8_t, and std::int8_t however, if I use short, int, unsigned int, std::uint16_t, or std::int16_t it appears to be printing out the hex values...

When using integral types that are not of char type... I am getting this output:

7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 30 30 30 30 30 30 30 30 30 30 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 79 79 79 79 79 79 79 79 79 79 33 33 33 33 33 33 33 33 33 33 5b 5b 5b 5b 5b 5b 5b 5b 5b 5b 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 70 70 70 70 70 70 70 70 70 70 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 30 30 30 30 30 30 30 30 30 30 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 79 79 79 79 79 79 79 79 79 79 33 33 33 33 33 33 33 33 33 33 5b 5b 5b 5b 5b 5b 5b 5b 5b 5b 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 70 70 70 70 70 70 70 70 70 70 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 30 30 30 30 30 30 30 30 30 30 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 79 79 79 79 79 79 79 79 79 79 33 33 33 33 33 33 33 33 33 33 5b 5b 5b 5b 5b 5b 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 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 79 6d 30 7e 7b 7f 70 5f 5b 33 79 6d 30 7e 7b 7f 70 5f 5b 33 79 6d 30 7e 7b 7f 70 5f 5b 33 79 6d 30 7e 7b 7f 70 5f 5b 33 79 6d 30 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 7f 7b 7e 30 6d 79 33 5b 5f 70 6d 6d 6d 6d 6d 6d 6d 6d 6d 30 30 30 30 30 30 30 30 30 30 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7b 7b 7b 7b 7b 7b 7b 7b 7b 7b 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 70 70 70 70 70 70 70 70 70 70 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5b 5b 5b 5b 5b 5b 5b 5b 5b 5b 33 33 33 33 33 33 33 33 33 33 79 79 79 79 79 79 79 79 79 79 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 30 30 30 30 30 30 30 30 30 30 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 7e 30 30 30 30 30 30 30 30 30 30 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 79 79 79 79 79 79 79 79 79 79 33 33 33 33 33 33 33 33 33 33 5b 5b 5b 5b 5b 5b 5b 5b 5b 5b 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 70 70 70 70 70 70 70 70 70 70 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7b 7b 7b 7b 7b 7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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

I think this might solve my own problem, but I would still like to know why I was getting the output for using char types with std::hex...

13
  • "Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<." en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt Commented Mar 7, 2020 at 6:01
  • 1
    en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2 "After constructing and checking the sentry object, inserts the character ch." So calling << on a char or unsigned char just outputs the char to the stream. The basefield (which std::hex modifies) is not consulted at all. Commented Mar 7, 2020 at 6:03
  • If you WANT to use the integer-like ostream& << overload for types of char or unsigned char, you need to call it like file.operator<<('c') Commented Mar 7, 2020 at 6:03
  • @John that appears to make sense. Sometimes, it's the simple things that you forget about or overlook. I was shaking my head for almost a half hour on this one for something simple... Then it dawned on me to try the same program with different types... Commented Mar 7, 2020 at 6:04
  • 1
    I'm still scratching my own head as to why you get those non-ascii characters in the output. 0x7e is a ~. First character should be a ~. Commented Mar 7, 2020 at 6:05

2 Answers 2

1

This is a bug in Notepad.

The core of the problem is that your text file is getting misinterpreted by Notepad as UTF-16 instead of as ascii.

enter image description here

enter image description here

Notepad has an algorithm that attempts to infer what type of encoding a file uses. The actual Windows API it invokes is called, IsTextUnicode. That API basically checks for a byte-ordering-mark (BOM) header. In the absence of a BOM, it reads the first few hundred bytes of text and does some inference and heuristics to detect if the file is ascii, utf-8, or unicode. It's essentially guessing.

In you case, the first byte of the file is 0x7E or ~. If I delete that in a hex editor and save it back, Notepad shows the correct file encoding and looks like this.
enter image description here

Something about that initial byte sequence of upper ascii characters throws the algorithm off.

So the correct fix would be to insert a Byte Order Mark into your text stream so Notepad won't try to infer the encoding based on heuristics.

file.open("display.txt", std::ios::out); const char* utf7_bom = "+/v8"; file << utf7_bom; 

That little 4-byte sequence tells text decoders, which most editors will recognize, that "this file is ascii". You can read more about BOM tokens here: https://en.wikipedia.org/wiki/Byte_order_mark

Sign up to request clarification or add additional context in comments.

5 Comments

Didn't think about passing it to a stringstream first then push it into the file stream for writing... This is better than the comment suggestion of using an explicit cast... For my general purposes it can be an int 64 haha... All I care about is getting the correct hex values in the text file!
Actually, I completely revised my answer to indicate that this is a bug in Notepad, not your code per se. You can still do the stringstream trick, but I suspect it will yield the same issue.
Maybe, but I still think the overall issue pertains to how C++ treats char types with output streams. For example if I have: char c = 'a'; std::cout << c; It will print out a to the console or text file... now if you want the integer version of a all you have to do is use integer promotion. std::cout << +c; and you are done! Now you will have the actual integer value on the character encoding type, either it be ascii, utf-8, etc...
In truth I don't think it has anything to do directly with the stream types such as iostream and fstream. I think this is part of the inner workings of the <<() and >>() operators that work on stream objects.
However, what you have suggested about the Byte Order mark and how text editors look for them in the header of the file upon opening, is very useful and a good piece of information to be aware of!
0

I had completely forgotten about the + operator for integer promotion of basic char types. The simplest, easiest and cleanest of all fixes in my opinion is to adjust my code to look like this:

int main() { char char digits[] = { 0x7e, 0x30, 0x6d, 0x79, 0x33, 0x5b, 0x5f, 0x70, 0x7f, 0x7b }; std::fstream file; file.open( "display.txt, std::ios::out ); for (int val = 0; val <= 255; val += 1) file << std::hex << +digits[val % 10] << " "; file << std::endl; // do the same for the rest of outputs to file... return 0; } 

All I did here was add the + before each of the chararrays`.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.