uint64_t

Aug 16, 2012 at 11:44am
Hello to all,

I have a uint64_t variable and when I do:

1
2
3
4
5
uint64_t it = 100;

char msgname[2000];
sprintf(msgname, "Transport:%d+%d-", it, outGateBaseId+i);


It appears to have a format error because of uint64_t. Instead of %d I have already tried %i, %ll but without success.

Can someone advise me.

Regards
CMarco
Aug 16, 2012 at 11:53am
Try %u for unsigned int, not sure if works with long long, which is what uint64_t is (if not, just long)
Aug 16, 2012 at 11:54am
Use stringstreams. Then you won't have to worry about that kind of stuff anymore, at all.
1
2
3
uint64_t it = 100;
stringstream msgname;
msgname<<"Transport:"<<it<<'+'<<outGateBaseId+i;
Aug 16, 2012 at 11:56am
1
2
uint64_t i = 5;
printf ( "% PRIu64 ", i ) ;


and for that you have to include
#include <inttypes.h>
Aug 16, 2012 at 11:57am
I wasn't sure whether the OP's code had to be C.
Aug 16, 2012 at 11:59am
Have you tried %lu or %llu?

Or maybe this will work:
sprintf(msgname, "Transport:%"PRIu64"+%d-", it, outGateBaseId+i);
Aug 16, 2012 at 12:24pm
Or simply use stringstreams and forget about all the trouble!
Aug 16, 2012 at 1:26pm
Hello

Thanks to all for the tips.

With the include and %llu the problem was solved.
The stringstreams solutions solve this also.

Regards
Aug 16, 2012 at 2:03pm
In C++ type name uint64_t is declared in header <cstdint> and is usually a typedef for unsigned long long. So you can use format specifier ll
From the C Standard

ll (ell-ell) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long long int or unsigned long long int argument; or that a
following n conversion specifier applies to a pointer to a long long int
argument.
Aug 16, 2012 at 2:08pm
uint64_t is unsigned long on one of the production systems at my work. PRIu64 is the right solution, for C.
Topic archived. No new replies allowed.