简体   繁体   中英

How to convert binary to decimal? (In a very simple way)

I need to write a program that converts binary numbers to decimal.

I'm very new to C++ so I've tried looking at other people's examples but they are way too advanced for me.. I thought I had a clever idea on how to do it but I'm not sure if my idea was way off or if I'm just missing something.

int main(void)
{
    //variables
    string binary;
    int pow2 = binary.length() - 1;
    int length = binary.length();
    int index = 0;
    int decimal = 0;

    cout << "Enter a binary number ";
    cin >> binary; cout << endl;

    while(index < length)
    {
      if (binary.substr(index, 1) == "1")
      {
        decimal = decimal + pow(2, pow2);
      }

      index++;
      pow2--;
    }

    cout << binary << " converted to decimal is " << decimal;
}

Your computer is a logical beast. Your computer executes your program, one line at a time. From start to finish. So, let's take a trip, together, with your computer, and see what it ends up doing, starting at the very beginning of your main :

string binary;

Your computer begins by creating a new std::string object. Which is, of course, empty. There's nothing in it.

int pow2 = binary.length() - 1;

This is the very second thing that your computer does.

And because we've just discovered that binary is empty, binary.length() is obviously 0, so this sets pow2 to -1. When you take 0, and subtract 1 from it, that's what you get.

int length = binary.length();

Since binary is still empty, its length() is still 0, and this simply creates a new variable called length , whose value is 0.

int index = 0;
int decimal = 0;

This creates a bunch more variables, and sets them to 0. That's the next thing your computer does.

cout << "Enter a binary number ";
cin >> binary; cout << endl;

Here you print some stuff, and read some stuff.

while(index < length)

Now, we get into the thick of things. So, let's see what your computer did, before you got to this point. It set index to 0, and length to 0 also. So, both of these variables are 0, and, therefore, this condition is false, 0 is not less than 0. So nothing inside the while loop ever executes. We can skip to the end of your program, after the while loop.

cout << binary << " converted to decimal is " << decimal;

And that's how you your computer always gives you the wrong result. Your actual question was:

sure if my idea was way off or if I'm just missing something.

Well, there also other problems with your idea, too. It was slightly off. For starters, nothing here really requires the use of the pow function. Using pow is like try to kill a fly with a hammer. What pow does is: it converts integer values to floating point, computes the natural logarithm of the first number, multiplies it by the second number, and then raises e to this power, and then your code (which never runs) finally converts the result from floating point to integer, rounding things off. Nothing of this sort is ever needed in order to simply convert binary to decimal. This never requires employing the services of natural logarithms. This is not what pow is for.

This task can be easily accomplished with just multiplication and addition. For example, if you already have the number 3, and your next digit is 7, you end up with 37 by multiplying 3 by 10 and then adding 7. You do the same exact thing with binary, base 2, with the only difference being that you multiply your number by 2, instead of 10.

But what you're really missing the most, is the Golden Rule Of Computer Programming :

A computer always does exactly what you tell it to do, instead of what you want it to do.

You need to tell your computer exactly what your computer needs to do. One step at a time. And in the right order. Telling your computer to compute the length of the string before it's even read from std::cin does not accomplish anything useful. It does not automatically recompute its length, after it's actually read. Therefore, if you need to compute the length of an entered string, you computer it after it's been read in, not before. And so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM