Converting binary string to decimal

In this section, we are going to write a code to convert binary string into a decimal number. To do this, we are going to use 2 to the power of n formula (2n) to get the decimal value. We will multiply the last digit of the binary digit with 20, then multiply the next binary digit with 21, and so on.

Let's use our binary digit from the previous section, which is 111110100, and convert it to a decimal number. Please see the following diagram to find out the answer:

We will get the decimal number by adding all the results of 2n multiplication. So, 256 + 128 + 64 + 32 + 16 + 0 + 4 + 0 + 0 equals 500.

Now, let's ...

Get C++ Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.