I am trying to convert an array of binary characters to Decimal. My code looks something like this:
int main(int argc, char** argv) {
int STRING_SIZE = 32;
int stringLength = 0;
char input[STRING_SIZE];
int arrayCounter = 0;
int base = 0;
char decimalValue[stringLength];
int exponent = 0;
int remainder[stringLength];
printf("\nPlease enter a Binary, Decimal or Hexadecimal value no longer than 32 characters. ");
scanf("%s", input);
fgetc(stdin);
//Count the length of the string.
while (input[stringLength] != 0) {
printf("%c", input[stringLength]);
stringLength++;
}
for (arrayCounter = 0; arrayCounter != stringLength; arrayCounter++) {
remainder[arrayCounter] = input[arrayCounter] - '0' / 10;
decimalValue[arrayCounter] = decimalValue[arrayCounter] + remainder[arrayCounter] * base;
input[arrayCounter] = input[arrayCounter] / 10;
base = base * 2;
printf("%c", decimalValue[arrayCounter]);
}
printf("%s", decimalValue);
return (EXIT_SUCCESS);
}
I know how to convert between binary and Decimal, but I am extremely confused on how to convert binary to decimal when the input is an array of characters.
input[arrayCounter]from ASCII to decimal, e.g.input[arrayCounter] -= '0', you do forremainder, but not for the stored character. (you should use another variable also rather than changing the character value -- but that's up to you)