Convert binary string to int c++ – C++ Program to Convert Binary Number to Decimal Number

Convert binary string to int c++: In the previous article, we have discussed about C++ Program to Print ASCII Value of a Character. Let us learn how to Convert Binary Number to Decimal Number in C++ Program.

Methods to Convert Binary to Decimal Numbers in C++

C++ convert binary to decimal: In this article, we will discuss different methods of converting binary numbers to decimal numbers in c++. The list of the methods that we will discuss is given below.

Let discuss the basic idea for converting binary to a decimal number which will help us later in writing the code. Let discuss the approach with the help of an example.

Suppose the binary of a number is 111 then its decimal form is 7. We will write 111 as 1*(2^2)+1*(2^1)+1*(2^0). So here we see we extract the digit of decimal number and multiply the digit with the power of 2 in increasing order. This means the first digit is multiplied with 2 to the power 0 while the second digit is multiplied with 2 to the power 1 and so on. So this is the intuition. Let’s discuss different methods to do it.

Method 1-Using loop

In this approach, we will take the help of a loop and modulo(%) and division(/) operator to extract the digit of the binary number. When we extract the digit we will simply multiply the digit with a power of 2 and stored the result in the variable.

Note: The program works only with binary numbers in the range of integers. In case you want to work with long binary numbers like 20 bits or 30 bit, you can use a string

Let’s write the code for this.

#include <bits/stdc++.h>
using namespace std;


int convertBinaryToDecimal(long long n)
{
    int decimalNumber = 0, i = 0, rem;
    while (n!=0)
    {
        rem = n%10;
        n /= 10;
        decimalNumber += rem*pow(2,i);
        i++;
    }
    return decimalNumber;
}

int main()
{
    long long n=111;
    cout << n << " in binary is " << convertBinaryToDecimal(n) << " in decimal";
    return 0;
}

Output

111 in binary is 7 in decimal

Method 2- Using pre-defined function

In c++ there is a pre-defined function that is used to convert binary to decimal numbers. Here we are talking about stoi() function. Let us see what the stoi() function is.

stoi() stands for the string to integer, it is a standard library function in C++ STL, it is used to convert a given string in various formats (like binary, octal, hex, or a simple number in string formatted) into an integer.

So one thing is clear that instead of an integer we have to pass a binary string in it. So this will also solve the problem of range that we will be going to face in the first method. Let’s write the code for this.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string n="111";
    cout << n << " in binary is " <<  stoi(n, 0, 2) << " in decimal";
    
    return 0;
}

Output

111 in binary is 7 in decimal

So these are the methods to convert binary number to decimal number in c++.