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:
- Scan the hexadecimal number from left to right digit by digit.
- Convert each digit of hexadecimal number into its corresponding 4-digit binary number, combine them and store it into result.
- 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:
- Program to convert hexadecimal number to octal number.
- Program to convert hexadecimal number to decimal number.
- Program to convert binary number to octal number.
- Program to convert binary number to decimal number.
- Program to convert binary number to hexadecimal 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 find Power of the Number.
- Program to find Quotient and Remainder.
- Program to find largest amongst three numbers.
- Program to find factorial of a number.
- Program to find GCD of two numbers.
- Program to find LCM of two numbers.
- Program to check whether entered number is odd or even.
- Program to check whether entered number is prime number or not.
- Program to check whether entered number is palindrome or not.