Thread: Binary Number to Decimal

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    20

    Binary Number to Decimal

    Hello all.

    I am new to programming and taking my first programming course which is basic C++ programming. However I am stuck on a exercise and not sure what to do.

    The exercise is: Write a program that asks the user to input a 4 digit binary number and print its decimal equivalent.

    Like I said this is a basic course and only a month in, so still new to things right now.

    Right now I have

    Code:
     #include<iostream>
    
    using namespace std;
    
    int main()
    
    {
        int binary, decimal;
        
        cout << "Enter a 4-digit binary number: " ;
        cin >> binary;
        
        decimal = 
        
        cout << "The decimal equivalent of " << binary << " is " << decimal << endl;
        
        system("pause");
        return 0;
        
    }
    I am not sure what to do to make it so it would convert it to decimal. I know how to convert binary to decimal (To convert 1101 it is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or 13).

    If someone could give me some help on what my calculation would be to convert it, that would be great.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    That way, you read the number as decimal. You have to input the number as a string and use strtol().
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    Try preventing pausing the system because it only works on certain operating systems. Try 'cin.get()' instead if you want a user to be able to view the output.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    I just need to know how to change what the user puts as binary to decimal. I know how to calculate binary to decimal but I only know that when I know what the binary number is, and the user can put any binary number.

    The reason for system("pause"); is because I use Dev C++ and you need it to hold the exe window open.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just loop through each bit of the int:
    Code:
    for(int i = 0;i < sizeof(binary) * 8;++i)
    {
      int singleBit = (binary >> i) & 1;
      // Do your sum calcuation here
    }
    If you understand what you're doing, you're not learning anything.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You explained it yourself when you asked the question, I think.
    (To convert 1101 it is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or 13)
    So the other thing you would have to do prior is break up a number like 1101.

    You can find out the value of every place.

    1101 % 10000 / 1000 = 1
    1101 % 1000 / 100 = 1
    1101 % 100 / 10 = 0
    1101 % 10 = 1

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by whiteflags View Post
    You explained it yourself when you asked the question, I think.

    So the other thing you would have to do prior is break up a number like 1101.

    You can find out the value of every place.

    1101 % 10000 / 1000 = 1
    1101 % 1000 / 100 = 1
    1101 % 100 / 10 = 0
    1101 % 10 = 1
    Well my program has to let the user enter whatever 4 digit binary number he wants. So how do I calculate the binary number the user chocies, and convert it to decimal?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It works for any binary number up to 4 digits you can think of, as well as others, if you bothered to compute the other place values. I'd prove it to you, but at this point you know everything you need to, so do it and see for yourself.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by whiteflags View Post
    It works for any binary number up to 4 digits you can think of, as well as others, if you bothered to compute the other place values. I'd prove it to you, but at this point you know everything you need to, so do it and see for yourself.
    I think I'm getting myself more confused. How do I take those numbers and actually make it work?

    I've been on this all night and haven't gotten anywhere.

    Here is the code I have and the problem with it.
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    
    {
        int binary1, binary2, binary3, binary4, decimal1, decimal2, decimal3, decimal4, total;
        
        cout << "Enter a 4-digit binary number: " ;
        cin >> binary1 >> binary2 >> binary3 >> binary4;
        
        decimal1 = binary4 * 1;
        decimal2 = binary3 * 2;
        decimal3 = binary2 * 4;
        decimal4 = binary1 * 8;
        total = decimal1 + decimal2 + decimal3 + decimal4;
        
        cout << "The decimal equivalent of " << binary4 << binary3 << binary2 << binary1 << " is " << total << endl;
        
        return 0;
    }
    So in this code I wrote it will convert any binary number to decimal, however the problem is the user has to put the binary number in 1 number at a time, with either a space or enter.

    I don't know how to do the calculation with just one statement. So the user could just enter the number as one number, instead of 4.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Read the binary number in as a string and then loop through the characters.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That bit of math I showed you computes place values.
    Quote Originally Posted by whiteflags View Post
    1101 % 10000 / 1000 = 1
    1101 % 1000 / 100 = 1
    1101 % 100 / 10 = 0
    1101 % 10 = 1
    All you really have to do is plug in those math expressions, on the left, into the formula you already know, which is
    (To convert 1101 it is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or 13)
    and the 1101, used as an example by both of us, is really an integer named binary.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I would read it into a string and process that. As you inspect each character you first multiply the running total by two and then add one to the running total if the char is a '1' (but not if it is a '0').
    Then you just cout the running total.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    You could just use bit operators:
    Code:
            string binary_string = "1010";
    	for(int i = 0; i < (int)real_binary_string.length(); i++){
    		ch = binary_string[i];
    		if(ch == '1'){
    			dec_number = dec_number | (int)pow((double)2, i);
    		}
    	}
            cout << dec_number << endl;
    -- Will you show me how to c++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. Replies: 3
    Last Post: 07-04-2008, 12:39 PM
  3. Decimal to Binary Conversion program
    By acidbeat311 in forum C Programming
    Replies: 5
    Last Post: 01-12-2006, 10:26 PM
  4. decimal to binary
    By kurz7 in forum C Programming
    Replies: 8
    Last Post: 07-10-2003, 12:03 AM
  5. Printing total of 1's in binary number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 02:50 PM