All submissions

Convert Binary String to Integer Without strtol function or Libraries

Code

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

uint16_t binary_to_uint(const char *str) {
    int num = 0,fact = 1,i=0;
    while(str[i] != 0){
        i++;
    }
    i-=1;
    for(;i>=0;i--){
        num += (str[i] - 48)*fact;
        // printf("%d ",num);
        fact= fact*2;
        // i++;
    }
    return num;
}

int main() {
    char bin[20];
    scanf("%s", bin);

    printf("%u", binary_to_uint(bin));
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

1010

Expected Output

10