Ritashugisha
Ritashugisha

Reputation: 93

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.

Upvotes: 0

Views: 627

Answers (2)

halex
halex

Reputation: 16403

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

Upvotes: 4

Lee Daniel Crocker
Lee Daniel Crocker

Reputation: 13196

Your strtoll() is fine. It's your %x that's wrong:

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

Upvotes: 2

Related Questions