C++ Program to Convert Hexadecimal to Binary

In this article, you will learn and get code on hexadecimal to binary conversion in C++. But if you're not aware of the simple steps and formula used for the conversion, then you can refer to hexadecimal to binary to get all the required things.

Hexadecimal to Binary in C++

To convert a hexadecimal number to a binary number in C++ programming, you have to ask the user to enter the hexadecimal number first. and then convert it into its equivalent binary value and print the equivalent binary value as output.

You can create the program in two ways:

Print the binary equivalent of a given hexadecimal number in C++

This program uses switch case to match and print the binary equivalent of each and every hex digit.

#include<iostream>
using namespace std;
int main()
{
    int i=0;
    char hexDecNum[10];
    cout<<"Enter the Hexadecimal Number: ";
    cin>>hexDecNum;
    cout<<"\nEquivalent Binary Value = ";
    while(hexDecNum[i])
    {
        switch(hexDecNum[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<<"--Invalid Hex Digit ("<<hexDecNum[i]<<")--";
        }
        i++;
    }
    cout<<endl;
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is its sample run:

C++ program convert hexadecimal to binary

Now supply a hexadecimal number input, say 2AD3, and press the ENTER key to print its equivalent binary value as shown in the output given below:

hexadecimal to binary c++

The following is a dry run of the above program with hexadecimal input, say 2AD3:

What if the user enters an invalid hexadecimal digit?

In this case, if the user enters an invalid hexadecimal digit, For example, if the user enters hexadecimal input in the following ways (containing one or more invalid hex digits, with or without valid ones):

If the program (created above) finds any hexadecimal digit (regardless of where or how it appears), a message --Invalid Hex Digit (HexDigit)-- is printed in such a way that:

Here is the sample output for the second case:

c++ hexadecimal to binary

Note: The above program doesn't convert a given hexadecimal number into its equivalent binary value. It simply prints the binary equivalent using switch case.

Now let's create another program that actually converts the number from hexadecimal to binary.

Convert Hexadecimal to Binary in C++

The strcat() function is used in this program to concatenate the binary equivalent of each and every hexadecimal digit (hex digit) one by one to a variable called binaryNum[]. And finally, it prints its value as output.

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    int i=0, chk=0;
    char hexDecNum[10], binaryNum[40]="";
    cout<<"Enter the Hexadecimal Number: ";
    cin>>hexDecNum;
    while(hexDecNum[i])
    {
        switch(hexDecNum[i])
        {
            case '0':
                strcat(binaryNum, "0000");
                break;
            case '1':
                strcat(binaryNum, "0001");
                break;
            case '2':
                strcat(binaryNum, "0010");
                break;
            case '3':
                strcat(binaryNum, "0011");
                break;
            case '4':
                strcat(binaryNum, "0100");
                break;
            case '5':
                strcat(binaryNum, "0101");
                break;
            case '6':
                strcat(binaryNum, "0110");
                break;
            case '7':
                strcat(binaryNum, "0111");
                break;
            case '8':
                strcat(binaryNum, "1000");
                break;
            case '9':
                strcat(binaryNum, "1001");
                break;
            case 'A':
            case 'a':
                strcat(binaryNum, "1010");
                break;
            case 'B':
            case 'b':
                strcat(binaryNum, "1011");
                break;
            case 'C':
            case 'c':
                strcat(binaryNum, "1100");
                break;
            case 'D':
            case 'd':
                strcat(binaryNum, "1101");
                break;
            case 'E':
            case 'e':
                strcat(binaryNum, "1110");
                break;
            case 'F':
            case 'f':
                strcat(binaryNum, "1111");
                break;
            default:
                chk = 1;
                break;
        }
        i++;
    }
    if(chk==0)
        cout<<"\nEquivalent Binary Value: "<<binaryNum;
    else
        cout<<"\nInvalid Hexadecimal Digit";
    cout<<endl;
    return 0;
}

Here is its sample run with a hexadecimal number input as AabB:

hexadecimal to binary conversion c++

The same program in different languages

C++ Quiz


« Previous Program Next Program »


Liked this post? Share it!