Search code examples
cstringhexstring-literals

C — Hex string to literal hex value


What is the best way (in C) to convert a hex string (of length 16 or less) to a literal hex value? For example...

#include <stdio.h>
void main() {
    char hexstring[] = "30f400010000";
    printf("0x%.16x", strtoll(hexstring, NULL, 16));
}

...prints 0x0000000000010000. I need this to print out 0x000030f400010000.

I'm very new to C, so I may be missing something very obvious. Thanks for any help.


Solution

  • You are using the wrong format specifier in your printf. The correct one for type long long to print the number in hexadecimal is %llx

    printf("0x%.16llx", strtoll(hexstring, NULL, 16));