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) {
    // Your logic here
    uint16_t res = 0;
    char c = *str;
    while(c!='\0'){
        res <<=4;
        if(c>='0' && c<='9') res|= (c-'0');
        else if(c>='A' && c<='F') res|= (c-'A' + 10);
        else if(c>='a' && c<='f') res|= (c-'a'+10);
        else return 0;
        c = *++str;
    }
    return res;
}

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

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

Solving Approach

 

 

 

Loading...

Input

1A3F

Expected Output

6719