Why do I get extra digits after the string of Hex digits when using printf?
cout << printf("%06X ", 0xABCDEF);
produces: ABCDEF 7
So where does the 7 come from and how can I get rid of it?
You need to use either cout or printf, not both.
printf("%06X ", 0xABCDEF);
Or
cout << hex << 0xABCDEF;
When you do both, the cout prints the result of the printf function, which is the number of characters printed (six characters and a space).
You're passing the result of the printf operation to cout.
Generally speaking, you use either printf or cout.
printf("%06X",0xABCDEF); //will do what you want in a C-like way
and
std::cout << std::hex << 0xABCDEF; //is the C++ iostream way
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