I’ve been trying to get a uint64 number into a string, in hex format. But it should include zeros. Example:
uint i = 1;
std::ostringstream message;
message << "0x" << std::hex << i << std::dec;
...
This will generate:
0x1
But this is not what I want. I want:
0x0000000000000001
Or however many zeros a uint64 needs.
You can use IO manipulators:
Alternatively, you can change the formatting flags separately on
std::coutwithstd::cout.setf(std::ios::hex)etc., e.g. see here (“Formatting”). Beware that the field width has to be set every time, as it only affects the next output operation and then reverts to its default.To get the true number of hex digits in a platform independent way, you could say something like
sizeof(T) * CHAR_BIT / 4 + (sizeof(T)*CHAR_BIT % 4 == 0 ? 0 : 1)as the argument forsetw.