Using `cout` to View Binary Data on Console
I'm writ开发者_如何学Cing a test application, and I'd like to keep it as simple as possible. I recall that there is a way to use cout
to print binary data to the console in ASCII format. For example:
int myVar = 0x1234;
cout << "My variable: 0x" << myVar << endl;
Hopefully, this would print to the console something like this:
My variable: 0x1234
Does anyone know how to properly modify the ios
(?) flags to print non-readable data to the console in ASCII format? Thanks.
You seem to want to print the binary data in hexadecimal format. This can be done using the hex manipulator:
#include <iostream>
using namespace std;
// ...
int myVar = 0x1234;
cout << "0x" << hex << myVar; // 0x1234
Note that this only works for integers.
精彩评论