I have something like
unsigned x = 16;
unsigned* p = &x;
std::cout << std::hex << std::setw(16) << std::setfill('0') << x << std::endl;
std::cout << std::hex << std::setw(16) << std::setfill('0') << p << std::endl;
output:
0000000000000010
000x7fffc35ba784
ostream::operator<< is overloaded for this? I can write this correctly with C, but I was wondering if there is a proper way to do this with iostream.
Use internal
like this:
#include <iostream>
#include <iomanip>
int main()
{
unsigned x = 16;
unsigned* p = &x;
std::cout << std::hex << std::setw(16) << std::setfill('0') << x << std::endl;
std::cout << std::hex << std::setw(16) << std::setfill('0') << p << std::endl;
std::cout << std::internal << std::hex << std::setw(16) << std::setfill('0') << p << std::endl;
}
This gives:
0000000000000010
000x7fffd123c1a4
0x007fffd123c1a4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With