Read a hexadecimal number (digits 0–9 and letters A–F or a–f, no 0x) and print its value in decimal.
Input
One hexadecimal number with 1 to 8 digits.
Output
Its decimal value.
Examples
Example 1 · Input
FF
Expected output
255
Example 2 · Input
1a
Expected output
26
Hints
Hint 1
Start with value = 0; for each digit, value = value * 16 + digit.
Hint 2
A digit '0'..'9' is c - '0'; 'A'..'F' is c - 'A' + 10; lowercase works the same way with 'a'.
Hint 3
Use long long: 8 hex digits can exceed an int.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
char hex[16];
scanf("%15s", hex);
long long value = 0;
for (int i = 0; hex[i] != '\0'; i++) {
char c = hex[i];
int d;
if (c >= '0' && c <= '9') d = c - '0';
else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
else d = c - 'A' + 10;
value = value * 16 + d;
}
printf("%lld\n", value);
return 0;
}