Browse Source

fixed function converting binary input to decimal not working for every input

remotes/origin/conversionOfNumbers
Laurin 11 months ago
parent
commit
2fe8d2dcbf
  1. 13
      src/conversionOfNumbers/conversionOfNumbers.c

13
src/conversionOfNumbers/conversionOfNumbers.c

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <string.h>
#include "conversionOfNumbers.h" #include "conversionOfNumbers.h"
@ -42,12 +43,12 @@ char* convertIntToBinaryStr(int input){
unsigned int convertBinaryStrToInt(char* input){ unsigned int convertBinaryStrToInt(char* input){
unsigned int result = 0; 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; return result;
} }

Loading…
Cancel
Save