Thread: Reading in Binary, Spitting out Decimal

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    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;
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    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;
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.
    Last edited by swoopy; 02-23-2004 at 04:22 PM.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    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 =)
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

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

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char binary[10];
    ... and how many characters are you going to fit into that array? (answer != 10)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    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.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by neandrake
    I thought you could. 0-9 and 10 for the \n.
    There is no 10.

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Ah...thanks swoopy. The code seems to work fine though.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 05:25 AM
  2. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 09:51 PM
  3. Decimal to binary conversion
    By blckspder in forum C Programming
    Replies: 5
    Last Post: 04-07-2005, 12:38 PM
  4. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  5. Converting decimal to binary?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-17-2002, 08:21 AM