Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra digits with printf Hex

Tags:

c++

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?

like image 370
DenverP Avatar asked Nov 27 '22 23:11

DenverP


2 Answers

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).

like image 139
Tmdean Avatar answered Dec 15 '22 01:12

Tmdean


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
like image 44
jkerian Avatar answered Dec 15 '22 01:12

jkerian