Binary to Hexadecimal Conversion

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

Example:

Binary = 100011011
Hexadecimal = 11B

The steps required to convert the Binary Number to Hexadecimal Number are as follows:

  1. Make sure the number of digits in binary number is divisible by 4. If not, add extra 0s in front.
  2. Divide the binary number into pairs of 4-digit binary number.
  3. Convert each 4-digit binary number pair to its Hexadecimal character.
  4. Combine all the Hexadecimal characters and store into temporary variable as ‘Hexadecimal’.
  5. Print the result.

C++ Program to Convert Binary to Hexadecimal is as follows:

/* C++ Program to Convert Binary Number to Hexadecimal Number*/  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan the Binary Number */  
    string Binary;  
    cout<<"Enter the Binary Number: ";  
    cin>>Binary;  
      
    /* Converting Binary to Hexadecimal*/  
      
    /* Make sure the number of digits in 'Binary'  
       Number is divisible by 4. If not, add  
       required 0s in front*/  
    while(Binary.length() % 4 != 0)  
    {  
        Binary.insert(0,"0");  
    }  
      
    /* Converting each digit of Binary number 
       into 4-digit binary number pair and convert 
       each pair to its corresponding Hexadecimal 
       character 
    */  
    string Hexadecimal = "";  
    for(int i = 0; i < Binary.length(); i=i+4)  
    {  
        string temp;  
        temp = temp + Binary[i];  
        temp = temp + Binary[i+1];  
        temp = temp + Binary[i+2];  
        temp = temp + Binary[i+3];  
        if(temp == "0000")  
            Hexadecimal += '0';  
        else if(temp == "0001")  
            Hexadecimal += '1';  
        else if(temp == "0010")   
            Hexadecimal += '2';  
        else if(temp == "0011")  
            Hexadecimal += '3';  
        else if(temp == "0100")  
            Hexadecimal += '4';  
        else if(temp == "0101")  
            Hexadecimal += '5';  
        else if(temp == "0110")  
            Hexadecimal += '6';  
        else if(temp == "0111")  
            Hexadecimal += '7';  
        else if(temp == "1000")  
            Hexadecimal += '8';  
        else if(temp == "1001")  
            Hexadecimal += '9';  
        else if(temp == "1010")  
            Hexadecimal += 'A';  
        else if(temp == "1011")  
            Hexadecimal += 'B';  
        else if(temp == "1100")  
            Hexadecimal += 'C';  
        else if(temp == "1101")  
            Hexadecimal += 'D';  
        else if(temp == "1110")  
            Hexadecimal += 'E';  
        else if(temp == "1111")  
            Hexadecimal += 'F';  
    }  
      
    /*Printing the result */  
    cout<<"The Hexadecimal of "<<Binary<<" is "<<Hexadecimal;  
}  

OUTPUT:
Enter the Binary Number: 100011011
The Hexadecimal of 000100011011 is 11B

Related Posts:

  1. Program to convert binary number to octal number.
  2. Program to convert binary number to decimal number.
  3. Program to convert octal number to binary number.
  4. Program to convert octal number to decimal number.
  5. Program to convert octal number to hexadecimal number.
  6. Program to convert decimal number to binary number.
  7. Program to convert decimal number to octal number.
  8. Program to convert decimal number to hexadecimal number.
  9. Program to convert hexadecimal number to binary number.
  10. Program to convert hexadecimal number to octal number.
  11. Program to convert hexadecimal number to decimal number.
  12. Program to check Leap Year.
  13. Program to find sum of first ‘n’ natural numbers.
  14. Program to Reverse a Number.
  15. Program to swap two Numbers.
  16. Program to print Fibonacci Series up to Nth Term.
  17. Program to find and print nth Fibonacci number.
  18. Program to find Power of the Number.
  19. Program to find Quotient and Remainder.
  20. Program to find largest amongst three numbers.

You may also like...

Leave a Reply

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