3 Ways To Convert Binary To Decimal In C++

Problem Statement:

Given a binary number as input, we have to write a solution to convert the binary number into an equivalent decimal number in C++.

Wish to enhance your foundation skills, then why not check out our Foundation Course designed by experienced mentors at Prepbytes.

So let’s Discuss, what is a Binary Number?
A binary number is defined as a number that is expressed in the binary system or base 2 numeral system. It describes numeric values by two separate symbols; 1 (one) and 0 (zero).

What is a Decimal Number?

Decimal is a term that describes the base-10 number system, probably the most commonly used number system. The decimal number system consists of ten single-digit numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. The number after 9 is 10. The number after 19 is 20 and so forth.

Example:
Input1: 111
Output1: 7
Explanation: (122) + (121)+(1*20) = 4 + 2 + 1 = 7

Input2: 1010
Output2: 10

Input3: 110
Output3: 6

Approach1: Conversion of Binary to Decimal using Recursion

In this approach we are using the recursive function. BinarytoDecimal(strBinary, length) which takes input string and its length and for each character convert it to decimal and multiply it with 2i . Add previous results to it.

  1. Take the input string strBinary[] containing a binary number.
  2. Calculate its length using strlen(strBinary).
  3. Function BinarytoDecimal(strBinary, length) takes input and returns the number calculated using a recursive approach.
  4. If we are at last character which is LSB, then return its decimal as it will be the same. (multiplied with 1 i.e 20 )
  5. Otherwise set temp=binary[i] – ‘0’. Its decimal value.
  6. Now multiply temp with 2len-i-1 using temp << len – i – 1.
  7. Add the result of other digits to temp using temp = temp + BinarytoDecimal(binary, len, i + 1).
  8. At the end of recursion return temp.
  9. Print the calculated decimal in main.

For Example:
If the binary number is 111.
dec_value = 1(22) + 1(21) + 1(20) = 7

The below diagram explains how to convert ( 1010 ) to equivalent decimal value:

Code Implementation of Recursive approach for converting Binary number into Decimal Number in C++:

#include<bits/stdc++.h>
using namespace std;
int BinarytoDecimal(char binary[],int len, int i=0){
   if (i == len-1)
   return (binary[i] - '0');

   int temp=binary[i]-'0';
   temp=temp<<len-i-1;
   temp=temp+BinarytoDecimal(binary,len,i+1);
   return (temp);
}
int main(){
   char strBinary[] = "111";
   int length=strlen(strBinary);
   cout <<"Decimal Number of given binary number: "<<BinarytoDecimal(strBinary,length) << endl;
   return 0;
}

Time Complexity: The time complexity of this implementation of converting binary number into decimal number is O(logN).

Space Complexity: The space complexity of this implementation of converting binary number into decimal number is O(1).

Approach 2: Conversion of Binary to Decimal using STL functions

For the conversion from Binary to Decimal number system, we will use stoi inbuilt function. In C++, the stoi() function converts a string to an integer value. The function is shorthand for “string to integer,” and C++ programmers use it to parse integers out of strings. The stoi() function is relatively new, as it was only added to the language as of its latest revision (C++11) in 2011.

To use stoi, you’ll need to provide specific criteria for the function:

stoi(string, position, int base)
The first criterion is the string that needs to be converted.
Next, we’ll need to specify the identifier for the starting position of the number within the string that needs to be parsed.
int base defines the numerical base for the string. As examples, we have 2 for binary, 16 for hexadecimal, and 10 for base 10.
Stoi function will directly convert binary strings to decimal numbers.

Now let’s see, the implementation using predefined functions of C++ for the conversion of binary number into decimal number:

#include <iostream>
using namespace std;

int main()
{
	char binaryNumber[] = "1010";
	
	cout << stoi(binaryNumber, 0, 2);

	return 0;
}

Time Complexity: The time complexity of this implementation of converting binary number into decimal number is O(N) where N is the length of the string.

Space Complexity: The space complexity of this implementation of converting binary number into decimal number is O(1).

Approach 3: Conversion of Binary to Decimal using For Loop

In this approach we are using the for loop.
Let’s Discuss the algorithm for converting binary numbers into decimal numbers.

  1. Firstly, initialize the decimal_num variable with value 0, and add the binary number in the bin_num variable, and initialize rem.
  2. Now, start a for loop with condition i = 0, bin_num != 0, ++i.
  3. In the loop block,
    a. Update the rem value to bin_num % 10.
    b. Change the value of bin_num with bin_num / 10.
    c. Atlast change the value of decimal_num with decimal_num + (rem) * (pow(2, i)).
  4. At the end of the for loop, print the decimal_num variable which is having the value of the decimal number of the given binary number.

Code Implementation of For Loop approach for converting Binary number into Decimal Number in C++:

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

int main() {
	int i, bin_num, decimal_num = 0, rem;  
    bin_num = 111;
    for (i = 0; bin_num != 0; ++i)  
    {  
        rem = bin_num % 10;  
        bin_num = bin_num / 10;  
        decimal_num = decimal_num + (rem) * ( pow (2, i));  
 
    }  
    cout<<"\n Decimal Number of given binary number:"<<decimal_num;
	return 0;
}

Time Complexity: The time complexity of this implementation of converting binary number into decimal number is O(logN).

Space Complexity: The space complexity of this implementation of converting binary number into decimal number is O(1).

This article tried to discuss a different approach to convert binary to decimal C++. Hope this blog helps you understand and solve the problem. To practice more problems you can check out MYCODE | Competitive Programming at Prepbytes.

Leave a Reply

Your email address will not be published. Required fields are marked *