Basically I am trying to convert hex strings into unsigned long long values
using strtoull
. Here's the simple code
#include <stdio.h>
#include <string.h>
int main ()
{
unsigned long long val =0;
//printf("sizeof(unsigned long long)=%d\n", sizeof(unsigned long long));
val = strtoull("0x8004298c42ULL",NULL,16);
printf("%llx\n", val);
if ( val == 0x8004298c42ULL)
{
printf("Success\n");
}
return 0;
}
I expect val
to print 8004298c42
but it prints 4298c42
. The 8
is being chopped off. I tried without the 0x
and without the ULL
too. But still the same result. Just to make sure that I am not missing out on some trivial printf
format specifier thing, I even checked the contents of val
. Still no use( ie Success was not printed )
I think I am missing out something trivial but don't know what!
The function strtoull()
is declared in <stdlib.h>
, not <string.h>
, so the compiler thinks it returns an int
, not an unsigned long long
.
Cure: make sure that all functions are declared before they are used. If you use gcc
, then you should consider some combination of -Wall
, -Wextra
, -Wstrict-prototypes
, -Wmissing-prototypes
, -Wold-style-declaration
, -Wold-style-definition
and -Werror
.