# Binary to Decimal converter

Page 1 of 1

## 2 Replies - 12000 Views - Last Post: 27 July 2008 - 10:46 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=58770&amp;s=bff1fc0f0c08ffdeebd63045ac82e3ec&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hbgrg

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 26-July 08

# Binary to Decimal converter

Posted 26 July 2008 - 10:51 PM

I am trying to convert from binary to decimal but no matter what 8 digit sequence I enter it gives me 255 (the binary equivalent to 11111111) please tell me what I am doing wrong!
```#include <iostream>
using namespace std;
void ConvToBin (char bit[100])
{
int i = 8;
int bin = 1;
int total = 0;
for (i; i > 0; i--)
{
if (bit[i] = 1)
{
total = bin + total;
}
bin = (bin * 2);
}
cout << total;
};
int main ()
{
char bit[100];
cout << "Please enter an eight digit binary sequence using only 1's and 0's: ";
cin >> bit;
ConvToBin (bit);
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Binary to Decimal converter

### #2 Martyr2

• Programming Theoretician

Reputation: 4625
• Posts: 12,677
• Joined: 18-April 07

## Re: Binary to Decimal converter

Posted 26 July 2008 - 11:19 PM

Well there are easier ways of converting from binary to decimal but I will work with what you have to get it to work for you...

```
#include <iostream>
using namespace std;
void ConvToBin (char bit[100])
{
int i = 7;
int bin = 1;
int total = 0;

// Starting at index 7 and going down to 0 (8 numbers)
for (i; i >= 0; i--)
{
// bit array here is a char array, so you have to compare against '1' not integer 1
if (bit[i] == '1')
{
total = bin + total;
}
bin = (bin * 2);
}
cout << total;
}

int main ()
{
char bit[100];
cout << "Please enter an eight digit binary sequence using only 1's and 0's: ";
cin >> bit;
ConvToBin (bit);
return 0;
}

```

Notice that we compare each bit from 0 to 7 not 8. Secondly, it is a char array so you have to compare against character '1' not integer 1. This should start then working for you.

Oh and congrats on being my 4000th post.

"At DIC we be 4000th posting ninjas, 4000 now, only 23.9 million more to go!"

### #3 hbgrg

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 26-July 08

## Re: Binary to Decimal converter

Posted 27 July 2008 - 10:46 AM

oooooo I get it thank you very much it makes sense now =]