All submissions

Convert Hexadecimal String to Integer Without strtol or sscanf functions

Code

#include <stdio.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
uint16_t hex_to_uint(const char *str) {
    // Your logic here
    int base = 1,l = strlen(str),d=0;
    for(int i = l-1;i>=0;i--)
    {
        if(str[i] >= '0' && str[i] <= '9'){
            d += (str[i] - '0')*base;
           
        }
        char c = toupper(str[i]);
        if(c >= 'A' && c <= 'Z')
        {
           d+= (c - 'A' + 10)*base;
        }
        base*=16;
    }
    return d;
}

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

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

Solving Approach

 

 

 

Loading...

Input

1A3F

Expected Output

6719