Hex printf is limited to 8 Fs
-
I am working on a long divide app and trying to see the complete value in hex. Here is the critical source.
uint64_t dividend; dividend = 0XFFFFFFFF; // count is 9 Fs printf( "with 8 Fs dividend is %llu, in hex %#020X \n", dividend, dividend ); dividend = 0XFFFFFFFFF; // count is 9 Fs printf( "with 9 Fs dividend is %llu, in hex %#020X \n", dividend, dividend ); dividend = 0XFFFFFFFFFF; // count is 10 Fs printf( "with 10 Fs dividend is %llu, in hex %#020X \n", dividend, dividend ); printf( "\n");
and here is the output
with 8 Fs dividend is 4294967295, in hex 0X0000000000FFFFFFFF with 9 Fs dividend is 68719476735, in hex 0X0000000000FFFFFFFF with 10 Fs dividend is 1099511627775, in hex 0X0000000000FFFFFFFF
A 64-bit number is 8 bytes. At 2 hex characters per byte that is 16 Fs. The printf output is limited to 8 Fs.
What is wrong with my printf statement? -
I am working on a long divide app and trying to see the complete value in hex. Here is the critical source.
uint64_t dividend; dividend = 0XFFFFFFFF; // count is 9 Fs printf( "with 8 Fs dividend is %llu, in hex %#020X \n", dividend, dividend ); dividend = 0XFFFFFFFFF; // count is 9 Fs printf( "with 9 Fs dividend is %llu, in hex %#020X \n", dividend, dividend ); dividend = 0XFFFFFFFFFF; // count is 10 Fs printf( "with 10 Fs dividend is %llu, in hex %#020X \n", dividend, dividend ); printf( "\n");
and here is the output
with 8 Fs dividend is 4294967295, in hex 0X0000000000FFFFFFFF with 9 Fs dividend is 68719476735, in hex 0X0000000000FFFFFFFF with 10 Fs dividend is 1099511627775, in hex 0X0000000000FFFFFFFF
A 64-bit number is 8 bytes. At 2 hex characters per byte that is 16 Fs. The printf output is limited to 8 Fs.
What is wrong with my printf statement?@Bryan-Kelly said in Hex printf is limited to 8 Fs:
dividend = 0XFFFFFFFFFF; // count is 10 Fs
You are wrong. See https://en.cppreference.com/w/cpp/language/integer_literal
Don't know what this has to do with Qt though.
-
Ok, and yes I am wrong. But please be a bit more precise. That page does not use items from stdint.h and does not use printf.
What does this have to do with Qt? Because that is what I am using and I don't see an error in my code.
And maybe it matters to state I am using a console app. -
Your problem is plain c. As the site tells you a number is by default an integer (32 bit) when you don't add a proper suffix.
-
This post is deleted!
-
@andr said in Hex printf is limited to 8 Fs:
Try %#020lX as format. Note the 'l'.
That's correct. I was stuck in old times. The suffix for big numbers is still useful though when using the 'auto' keyword 🙂.
So sorry, the correct link is https://cplusplus.com/reference/cstdio/printf/ -
As I read it, you added a character for l, lower case L, in front of the X. When I changed the code it made no difference. I tried the character 1, digit 1, and it made the zero count to be 201, but still ended with 8 Fs, not 9 or 10.
uint64_t dividend; dividend = 0XFFFFFFFF; // count is 9 Fs #ifdef SHOW_DETAILS printf( "with 8 Fs dividend is %llu, in hex %#020X \n", dividend, dividend ); #endif // changed the below adding character 1, one, made the output 201 characters dividend = 0XFFFFFFFFF; // count is 9 Fs #ifdef SHOW_DETAILS printf( "with 9 Fs dividend is %llu, in hex %#0201X \n", dividend, dividend ); #endif dividend = 0XFFFFFFFFFF; // count is 10 Fs // change below adding lower case letter L, no detecable difference. #ifdef SHOW_DETAILS printf( "with. 10 Fs dividend is %llu, in hex %#020lX \n", dividend, dividend ); printf( "\n"); #endif
-
MS Bing came up with the suggestion. It adds some includes
#include <stdio.h> #include <stdint.h> #include <inttypes.h>
and the new line
printf( "LINE %4i with 16 Fs dividend is %llu, in hex 0X%" PRIX64 "\n", __LINE__, dividend, dividend );
and the output
LINE 51 with 16 Fs dividend is 18446744073709551615, in hex 0XFFFFFFFFFFFFFFFF