1

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.

5
  • So do I understand it correctly, that you are trying to convert a c_string (char[]) representing a binary number into a new string representing a decimal number? Commented Mar 28, 2018 at 4:16
  • Yes, I want to convert the string into a new string representing the decimal value. E.g input: 10 (binary), output: 2 (decimal) Commented Mar 28, 2018 at 4:20
  • Is there some specific reason you want to implement this yourself? Otherwise, I recommend using strol() with base 2 [cplusplus.com/reference/cstdlib/strtol/] to turn the string to an integer, and then sprintf to turn the integer back to a string. Commented Mar 28, 2018 at 4:27
  • Part of an assignment, our teacher wants us to implement it ourselves. Commented Mar 28, 2018 at 4:31
  • You never convert input[arrayCounter] from ASCII to decimal, e.g. input[arrayCounter] -= '0', you do for remainder, but not for the stored character. (you should use another variable also rather than changing the character value -- but that's up to you) Commented Mar 28, 2018 at 4:34

1 Answer 1

4

There was similar question asked(Binary to Decimal and Hex (bit by bit) ) where I had replied to use formula below to convert binary chars to decimal

2^7 * (1 OR 0) + 2^6 * (1 OR 0) + 2^5 * (1 OR 0) + 2^4 * (1 OR 0) + 2^3 * (1 OR 0) + 2^2 * (1 OR 0) + 2^1 * (1 OR 0) + 2^0 * (1 OR 0)

Below is the sample code you can refer that does it, you can take bstr in input instead of initializing at declaration

#include<math.h>
#include<stdio.h>
#include<string.h>
int main()
{
char bstr[]="10011";
int i=0;
int len  = strlen(bstr);
int sum = 0;
int j = 0;

for(i=(len-1);i>=0;i--)
{
    sum = sum + (pow(2,i) * (bstr[j] - '0'));
    j++;
}
printf("decimal = %d\n",sum);
return 0;
}
Sign up to request clarification or add additional context in comments.

7 Comments

I tried to implement this, but I am receiving a different output. for (i= stringLength- 1; i >= 0; i--) { decimalValue = decimalValue + (power(2, i)) * (input[arrayCounter] - '0'); arrayCounter++; } printf("\n%d", decimalValue); INPUT: 10 Output: 112
Are you sure decimalValue is initialized to 0
Just checked, I had it initialized as a Char, changed it to Int and it is initialized to 0. Output now returns -144 when input 10.
arrayCounter is it initialized to zero? you would have to debug to see whats going wrong
Of course that was the problem! Thank you so much for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.