Hexadecimal to Binary Conversion

Hexadecimal to Binary Conversion” is one of the classic programming problem exercise. Here, we are given a hexadecimal number, entered by user and our task is to write a program to convert the given hexadecimal number to its equivalent binary number.

Example:

Hexadecimal Number: 32A
Binary Number: 1100101010

The steps required for hexadecimal to binary conversion are as follows:

  1. Scan the hexadecimal number from left to right digit by digit.
  2. Convert each digit of hexadecimal number into its corresponding 4-digit binary number, combine them and store it into result.
  3. Print the result.

C++ Program for hexadecimal to binary conversion is as follows:

/* C++ Program for Hexadecimal to Binary Conversion */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan the hexadecimal Number */  
    string Hexadecimal;  
    cout<<"Enter the Hexadecimal Number: ";  
    cin>>Hexadecimal;  
      
    /* Converting Hexadecimal to Binary*/  
    string Binary;  
    /* Converting each digit of hexadecimal number 
       to 4-digit binary number and store the  
       combined result into 'Binary'  
    */  
    for(int i = 0; i < Hexadecimal.length(); i++)  
    {  
        string temp;  
        switch(Hexadecimal[i])  
        {  
            case '0': temp = "0000";  
            break;  
            case '1': temp = "0001";  
            break;  
            case '2': temp = "0010";  
            break;  
            case '3': temp = "0011";  
            break;  
            case '4': temp = "0100";  
            break;  
            case '5': temp = "0101";  
            break;  
            case '6': temp = "0110";  
            break;  
            case '7': temp = "0111";  
            break;  
            case '8': temp = "1000";  
            break;  
            case '9': temp = "1001";  
            break;  
            case 'A': temp = "1010";  
            break;  
            case 'B': temp = "1011";  
            break;  
            case 'C': temp = "1100";  
            break;  
            case 'D': temp = "1101";  
            break;  
            case 'E': temp = "1110";  
            break;  
            case 'F': temp = "1111";  
        }  
        /* Combining the 4-digit binary numbers */  
        Binary = Binary + temp;  
    }  
      
    /* Truncating Extra 0s from beginning, if any */  
    while(Binary[0]=='0')  
    {  
        Binary.erase(Binary.begin()+0);  
    }  
      
    /*Printing the result */  
    cout<<"The Binary of "<<Hexadecimal<<" is "<<Binary;  
}  

OUTPUT:
Enter the Hexadecimal Number: 32A
The Binary of 32A is 1100101010

Related Posts:

  1. Program to convert hexadecimal number to octal number.
  2. Program to convert hexadecimal number to decimal number.
  3. Program to convert binary number to octal number.
  4. Program to convert binary number to decimal number.
  5. Program to convert binary number to hexadecimal number.
  6. Program to convert octal number to binary number.
  7. Program to convert octal number to decimal number.
  8. Program to convert octal number to hexadecimal number.
  9. Program to convert decimal number to binary number.
  10. Program to convert decimal number to octal number.
  11. Program to convert decimal number to hexadecimal number.
  12. Program to find Power of the Number.
  13. Program to find Quotient and Remainder.
  14. Program to find largest amongst three numbers.
  15. Program to find factorial of a number.
  16. Program to find GCD of two numbers.
  17. Program to find LCM of two numbers.
  18. Program to check whether entered number is odd or even.
  19. Program to check whether entered number is prime number or not.
  20. Program to check whether entered number is palindrome or not.

You may also like...