Program to Convert Hexadecimal to Binary C++

How to convert Hexadecimal to Binary C++:

To convert or change the hexadecimal number to binary number replace the each octal digits by a binary number using hexadecimal to binary chart.

convert hexadecimal to binary C++

For example:

We want to convert hexadecimal number 65B2 to binary. For this we will replace each hexadecimal digit to binary values using the above table:

Hexadecimal number:  6    5    B    2

Binary values:      (0110) (0101) (1011) (0010)

So (65B2)16 = (0110010110110010)2

Download Code:  

[adinserter block=”1″]

Download: hextobin.cpp

Program to Convert Hexadecimal to Binary C++:

#include<iostream>

using namespace std;
#define MAX 1000

int main()
{
    char binaryNumber[MAX],hexaDecimal[MAX];
    long int i=0;

    cout<<"Enter any hexadecimal number: ";
    cin>>hexaDecimal;

    cout<<"nEquivalent binary value: ";
    while(hexaDecimal[i])
    {
         switch(hexaDecimal[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': cout<<"1010"; break;
             case 'B': cout<<"1011"; break;
             case 'C': cout<<"1100"; break;
             case 'D': cout<<"1101"; break;
             case 'E': cout<<"1110"; break;
             case 'F': cout<<"1111"; break;
             case 'a': cout<<"1010"; break;
             case 'b': cout<<"1011"; break;
             case 'c': cout<<"1100"; break;
             case 'd': cout<<"1101"; break;
             case 'e': cout<<"1110"; break;
             case 'f': cout<<"1111"; break;
             default:  cout<<"nInvalid hexadecimal digit "<<hexaDecimal[i];
         }
         i++;
    }

    return 0;
}

 

OUTPUT:

convert hexadecimal to binary c++

Leave a Comment