All submissions

Convert Hexadecimal String to Integer Without strtol or sscanf functions

Code

#include <stdio.h>
#include <stdint.h>

uint16_t hex_to_uint(const char *str) {
    char *p = (char *)str;
    uint16_t ret = 0;
    while (*p) {
        ret <<= 4;
        if ((*p >= '0') && (*p <= '9'))
            ret |= *p - '0';
        else if ((*p >= 'a') && (*p <= 'f'))
            ret |= *p - 'a' + 10;
        else if ((*p >= 'A') && (*p <= 'F'))
            ret |= *p - 'A' + 10;
        p++;
    }
    return ret;
}

int main() {
    char hex[10];
    scanf("%s", hex);

    printf("%u", hex_to_uint(hex));
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

1A3F

Expected Output

6719