All submissions

Convert Hexadecimal String to Integer Without strtol or sscanf functions

Code

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

uint8_t convert_to_hex(char ch){
    if((ch>='0')&&(ch<='9')){
        return ch-'0';
    }else if((ch >= 'a')&&(ch <= 'f')){
        return ch-'a'+10;
    }else if((ch >= 'A')&&(ch <= 'F')){
        return ch-'A'+10;
    }else{
        return 0;
    }
}

uint16_t hex_to_uint(const char *str) {
    // Your logic here
    uint8_t i;
    uint16_t num = 0;
    for(i = 0;str[i]!='\0';i++){
        num = (num<<4) | (convert_to_hex(str[i]));
    }
    return num;
}

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

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

Solving Approach

 

 

 

Loading...

Input

1A3F

Expected Output

6719