# 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 to Hexadecimal Conversion):

Binary = 100011011Hexadecimal = 11B

The steps required for Binary to Hexadecimal Conversion are as follows:

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

**C++ Program for Binary to Hexadecimal Conversion is as follows:**

/* C++ Program for Binary to Hexadecimal Conversion*/ #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; }

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

**Related Posts:**

- Program to convert binary number to octal number.
- Program to convert binary number to decimal number.
- Program to convert octal number to binary number.
- Program to convert octal number to decimal number.
- Program to convert octal number to hexadecimal number.
- Program to convert decimal number to binary number.
- Program to convert decimal number to octal number.
- Program to convert decimal number to hexadecimal number.
- Program to convert hexadecimal number to binary number.
- Program to convert hexadecimal number to octal number.
- Program to convert hexadecimal number to decimal number.
- Program to check Leap Year.
- Program to find sum of first ‘n’ natural numbers.
- Program to Reverse a Number.
- Program to swap two Numbers.
- Program to print Fibonacci Series up to Nth Term.
- Program to find and print nth Fibonacci number.
- Program to find Power of the Number.
- Program to find Quotient and Remainder.
- Program to find largest amongst three numbers.