From 2fe8d2dcbffb0299212cfea71b6b7f647db8afb2 Mon Sep 17 00:00:00 2001 From: Laurin Date: Sat, 3 Feb 2024 10:40:56 +0100 Subject: [PATCH] fixed function converting binary input to decimal not working for every input --- src/conversionOfNumbers/conversionOfNumbers.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index fadc59a..337c1d8 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "conversionOfNumbers.h" @@ -42,12 +43,12 @@ char* convertIntToBinaryStr(int input){ unsigned int convertBinaryStrToInt(char* input){ unsigned int result = 0; - int x = 0; - while(input[x] != '\0'){ - if(input[x] == '1'){ - result += 1 * pow(2, x); - } - x++; + int index = strlen(input) - 1; + int exponent = 0; + while(index >= 0){ + if(input[index] == '1') result += 1 * pow(2, exponent); + index--; + exponent++; } return result; }