Reading in Binary, Spitting out Decimal

• 02-23-2004
neandrake
Reading in Binary, Spitting out Decimal
Yes, this is a homework assignment; not asking anyone to do it for me. The idea is to read in 10 digits, (1 or 0) and then calculate the decimal number. The code I have now works except that you have to put in exactly 10 digits (instead of "101", "0000000101").

I've been looking up cout.fill and stuff and I can't find out how to pad the variable with leading zeros. cout.fill only pads it for the output and doesn't pad the variable. I was thinking about sprintf() but not sure how to do it. The object of the homework was to convert binary to decimal, which I did, but I asked the professor and he says that he won't put in leading zeros. Any ideas anyone?

Code:

```#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() {     char binary[10];     double total = 0;     cout << "Enter a 10 digit series of zeros and ones: " << endl;     cin >> binary;     cout.fill('0');     cout << setw( 10 ) << binary << endl;     cout << binary << endl;     for (int i=0; i<10; i++) {         if (binary[i] == '1') {             total += pow((double)2, (double)(9-i));         }     }     cout << "\nThe number you entered was: " << total << endl;     cin >> total; }```
• 02-23-2004
Edward
When converting your array, walk back from the end. You can find the end with strlen. Then, instead of using a calculation, a variable marking the current bit level would be much easier.
Code:

```#include <iostream> #include <iomanip> #include <cmath> #include <cstring> using namespace std; int main() {     char binary[10];     double total = 0;     int base = 0;     cout << "Enter a 10 digit series of zeros and ones: " << endl;     cin >> binary;     cout.fill('0');     cout << setw( 10 ) << binary << endl;     cout << binary << endl;     for (int i=strlen(binary)-1; i>=0; i--, base++) {         if (binary[i] == '1') {             total += pow((double)2, (double)base);         }     }     cout << "\nThe number you entered was: " << total << endl; }```
• 02-23-2004
swoopy
Your code seems to display it fine, with leading zeros. If you want the actual variable to have leading zeros:

Edit: oops, thought variable needed padding.
• 02-23-2004
neandrake
Thanks Edward

I was toying with code like that earlier, but never thought to use a different variable for the base. As you could see, I was using variations of 'i' that were messing me up. BTW, I had been thinking about using strlen also, but to put that together with running through the array backwards was a combination I hadn't tried. Thanks again.

Swoopy, thanks for the help tho =)
• 02-23-2004
DougDbug
strtoul()
unsigned long strtoul( const char *start, char **end, int base ); // "String To Unsigned Long"... Any base from 2 to base-36.

Your instructor might consider it cheating... it's too easy!

The attached example uses strtoul() for input.
• 02-23-2004
Hammer
>>char binary[10];
... and how many characters are you going to fit into that array? (answer != 10)
• 02-23-2004
neandrake
Quote:

Originally posted by Hammer
>>char binary[10];
... and how many characters are you going to fit into that array? (answer != 10)

I thought you could. 0-9 and 10 for the \n.
• 02-24-2004
swoopy
Quote:

Originally posted by neandrake
I thought you could. 0-9 and 10 for the \n.
There is no 10.
• 02-24-2004
neandrake
Ah...thanks swoopy. The code seems to work fine though.
• 02-24-2004
hk_mp5kpdw
Another way to do it... use a bitset:
Code:

```#include <bitset> #include <string> #include <iostream> using namespace std; int main() {     string input;     cout << "Enter a series of zeroes and ones (up to 10 digits): ";     cin >> input;     bitset<10> value(input);  // Convert string input into bitset     cout << "The number you entered was: " << value.to_ulong() << endl;     return 0; }```
Sample output:
Code:

```Enter a series of zeroes and ones (up to 10 digits): 1101 The number you entered was: 13```
• 02-25-2004
Hammer
Quote:

Originally posted by neandrake
Ah...thanks swoopy. The code seems to work fine though.
That's the magic of buffer overflows, you can run for years and never know they exist.

>>I thought you could. 0-9 and 10 for the \n.<<
And what about the \0 ? Where will that go?

When using C style strings, you're actually using a char array that is null terminated. So if you have this:

>>char name[7];
you can store this in it:
hammer\0

If you have this:
>>char name[6];
and try to store the same name in it, the \0 will go outside the bounds of the array. This might cause a problem with the program immediately, it might never get picked up. Either way, it's still a bug and something you should never put in by design.