# Converting Binary to Decimal using strings

Page 1 of 1

## 7 Replies - 1858 Views - Last Post: 15 August 2012 - 11:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288965&amp;s=bc9bab17868e30dc58674725b2c26fec&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ma21212

Reputation: 0
• Posts: 4
• Joined: 20-July 12

# 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?
```#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;
}

```

Is This A Good Question/Topic? 0

## Replies To: Converting Binary to Decimal using strings

• Saucy!

Reputation: 6219
• Posts: 23,965
• Joined: 23-August 08

## 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 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,997
• Joined: 30-May 10

## Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:15 AM

You need to initialise sum.

### #4 Skydiver

• Code herder

Reputation: 4378
• Posts: 14,132
• Joined: 05-May 12

## Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:17 AM

Sum looks to be uninitialized.

### #5 ma21212

Reputation: 0
• Posts: 4
• Joined: 20-July 12

## 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 Skydiver

• Code herder

Reputation: 4378
• Posts: 14,132
• Joined: 05-May 12

## 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?

### #7 ma21212

Reputation: 0
• Posts: 4
• Joined: 20-July 12

## Re: Converting Binary to Decimal using strings

Posted 15 August 2012 - 11:32 AM

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

### #8 Skydiver

• Code herder

Reputation: 4378
• Posts: 14,132
• Joined: 05-May 12

## 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.