#include <stdio.h>
#include <stdint.h>
uint16_t hex_to_uint(const char *str) {
uint16_t result = 0;
char c;
uint8_t value;
for (int i = 0; str[i] != '\0'; i++) {
c = str[i];
// Convert lowercase to uppercase
if (c >= 'a' && c <= 'f') {
c -= 32;
}
// Convert character to numeric value
if (c >= '0' && c <= '9') {
value = c - '0';
} else if (c >= 'A' && c <= 'F') {
value = c - 'A' + 10;
} else {
// Invalid character
return 0;
}
result = (result << 4) | value; // Shift left 4 bits and add new digit
}
return result;
}
int main() {
char hex[10];
scanf("%9s", hex); // Limit input to 9 characters + null terminator
printf("%u\n", hex_to_uint(hex));
return 0;
}result = 0 to accumulate the final number.2. Loop Through Each Character
c in the input string:'a' → 'A')'a' - 32 = 'A''0' to '9': value = c - '0''A' to 'F': value = c - 'A' + 103. Shift and Add
result <<= 4result |= value4. Return the Result
result holds the final integer value
Input
1A3F
Expected Output
6719