C++ Program to Convert Hexadecimal Number to Binary

C++Server Side ProgrammingProgramming

Given with hexadecimal number as an input, the task is to convert that hexadecimal number into a binary number.

Hexadecimal number in computers is represented with base 16 and binary number is represented with base 2 as it has only two binary digits 0 and 1 whereas hexadecimal number have digits starting from 0 – 15 in which 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E and 15 as F.

To convert hexadecimal number into binary number every number is converted into its binary equivalent of 4 bits and after that these numbers are combined to form one corresponding binary number.

Example

Input-: 123B
   1 will have binary equivalent of 4 digit -: 0001
   2 will have binary equivalent of 4 digit -: 0010
   3 will have binary equivalent of 4 digit -: 0011
   B(11) will have binary equivalent of 4 digit -: 1011
Output-: 0001001000111011

Algorithm

Start
Step 1 -> declare function to convert Hexadecimal to Binary Number
   void convert(string hexa)
   Declare variable as long int i = 0
   Loop While(hexa[i])
      Use Switch (hexa[i])
         case '0':
            print "0000"
            break;
         case '1':
            print "0001"
            break;
         case '2':
            print "0010"
            break;
         case '3':
            print "0011"
            break;
         case '4':
            print "0100”
            break;
         case '5':
            print "0101"
            break;
         case '6':
            print "0110"
            break;
         case '7':
            print "0111"
            break;
         case '8':
            print "1000"
            break;
         case '9':
            print "1001"
            break;
         case 'A':
         case 'a':
            print "1010"
            break;
         case 'B':
         case 'b':
            print "1011"
            break;
         case 'C':
         case 'c':
            print "1100"
            break;
         case 'D':
         case 'd':
            print "1101"
            break;
         case 'E':
         case 'e':
            print "1110"
            break;
         case 'F':
         case 'f':
            print "111"
            break;
         default:
            print please enter valid hexadecimal digit
         End
      i++
   End
Step 2 -> In main()
   Declare string hexa = "123B"
   Print convert(hexa);
Stop

Example

#include <bits/stdc++.h>
#include<string.h>
using namespace std;
// convert Hexadecimal to Binary Number
void convert(string hexa){
   long int i = 0;
   while (hexa[i]){
      switch (hexa[i]){
      case '0':
         cout << "0000";
         break;
      case '1':
         cout << "0001";
         break;
      case '2':
         cout << "0010";
         break;
      case '3':
         cout << "0011";
         break;
      case '4':
         cout << "0100";
         break;
      case '5':
         cout << "0101";
         break;
      case '6':
         cout << "0110";
         break;
      case '7':
         cout << "0111";
         break;
      case '8':
         cout << "1000";
         break;
      case '9':
         cout << "1001";
         break;
      case 'A':
      case 'a':
         cout << "1010";
         break;
      case 'B':
      case 'b':
         cout << "1011";
         break;
      case 'C':
      case 'c':
         cout << "1100";
         break;
      case 'D':
      case 'd':
         cout << "1101";
         break;
      case 'E':
      case 'e':
         cout << "1110";
         break;
      case 'F':
      case 'f':
         cout << "1111";
         break;
      default:
         cout << "\please enter valid hexadecimal digit "<< hexa[i];
      }
   i++;
   }
}
int main(){
   string hexa = "123B";
   cout << "\nEquivalent Binary value is : ";
      convert(hexa);
   return 0;
}

Output

Equivalent Binary value is : 0001001000111011
raja
Published on 23-Sep-2019 14:36:46
Advertisements