#include <stdio.h> #include <string.h> #include <math.h> int main() { char string[100]; int s; char a; char j; int sum; printf("B = B to D\n"); printf("D = D to B\n"); printf("choose which one to convert to:"); scanf("%c%c", &a, &j); if (a == 'B') { printf("enter binary number to convert to decimal: "); scanf("%s", string); for(s = strlen(string)-1; s >= 0; s--) { if(string[s] == '1') { sum = sum + pow(2,s); } } printf("the decimal number is: %d\n", sum); } return 0; }

# Converting Binary to Decimal using strings

Page 1 of 1## 7 Replies - 2000 Views - Last Post: 15 August 2012 - 11:55 AM

### #1

# Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:00 AM

I've written a program that asks the user to input a number using strings, the program then will convert that number to decimal, however Im having a problem with it, when I compile (using -lm) and run the a.out, lets say I try to convert 11 to decimal it gives a huge number... not sure where I went wrong so would anyone help please?

##
**Replies To:** Converting Binary to Decimal using strings

### #2

## Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:14 AM

sum = sum + pow(2,s);

What is the initial value of sum the first time you use it in this expression?

### #3

## Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:15 AM

You need to initialise sum.

### #4

## Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:17 AM

Sum looks to be uninitialized.

### #5

## Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:23 AM

derp that fixes everything.... thank you. Now this is a akward thing to ask but, how would I be able to convert the other way? from decimal to binary, im guessing I need a remainder and and a % but Im not really sure, any help?

### #6

## Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:29 AM

Actually, before you go further, try the binary number "100" and see what result you get. I think you'll need to fix that problem first before you move on.

As for decimal to binary, how do you do it on paper? Can you articulate the steps you take? If so, then you can write a program that follows those steps.

Aren't you getting enough advice from here and here?

As for decimal to binary, how do you do it on paper? Can you articulate the steps you take? If so, then you can write a program that follows those steps.

Aren't you getting enough advice from here and here?

### #7

## Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:32 AM

Yeah your right, it only gives me odd numbers...

### #8

## Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:55 AM

The binary number "1101" is also odd, but I bet it also is giving you the incorrect answer. It's not a matter of odd or even, but the place values you are giving each digit.

Here's a hint: in decimal, a number 325 is: 3*100 + 2*10 + 5*1 = 3 * 10^2 + 2*10^1 + 5*10^0.

Step through your code and see how you are using the pow() function.

Here's a hint: in decimal, a number 325 is: 3*100 + 2*10 + 5*1 = 3 * 10^2 + 2*10^1 + 5*10^0.

Step through your code and see how you are using the pow() function.

Page 1 of 1