There are many questions regarding how to pad a fixed number of leading zeroes when using C++ streams with variables we want represented in hexadecimal format:
std::cout << std::hex << setfill('0') << setw(8) << myByteSizedVar;
My question regards how to do this for not a fixed width, but a multiple of some fixed amount - likely 8 for the obvious reason that when comparing outputs we might want:
0x87b003a
0xab07
To match up for width to be compared more easily (okay the top is larger - but try a bitwise comparison in your head? Easily confused.)
0x87b003a
0x000ab07
Nice, two bytes lined up nice and neatly. Except we only see 7 bits - which is not immediately obvious (especially if it were 15/16, 31/32, etc.), possibly causing us to mistake the top for a negative number (assuming signed).
All well and good, we can set the width to 8 as above.
However, when making the comparison next to say a 32-bit word:
0x000000000000000000000000087b003a
0x238bfa700af26fa930b00b130b3b022b
It may be more unneccessary, depending on the use, or even misleading if dealing with hardware where the top number actually has no context in which to be a 32-bit word.
What I would like, is to automagically set the width of the number to be a multiple of 8, like:
std::cout << std::hex << std::setfill('0') << setWidthMultiple(8) << myVar;
For results like:
0x00000000
0x388b7f62
0x0000000f388b7f62
How is this possible with standard libraries, or a minimal amount of code? Without something like Boost.Format
.