C++ Program to Convert Binary to Hexadecimal

In this article, you will learn and get code for binary to hexadecimal conversion in C++. The program is created with and without using user-defined functions.

But before going through the program, if you are not aware of the steps and formula used for binary to hexadecimal conversion, then you can go there to get all the things you need.

Binary to Hexadecimal in C++

To convert a binary number to hexadecimal in C++ programming, you have to ask the user to enter any number in the binary number system. Then convert it into its equivalent hexadecimal value. Print the hexadecimal value as output.

The question is: write a program in C++ to receive any binary number and print its equivalent hexadecimal value.  The answer to this question is given below:

#include<iostream>
using namespace std;
int main()
{
    int binaryNum, hex=0, mul=1, chk=1, rem, i=0;
    char hexDecNum[20];
    cout<<"Enter any Binary Number: ";
    cin>>binaryNum;
    while(binaryNum!=0)
    {
        rem = binaryNum%10;
        hex = hex + (rem*mul);
        if(chk%4==0)
        {
            if(hex<10)
                hexDecNum[i] = hex+48;
            else
                hexDecNum[i] = hex+55;
            mul = 1;
            hex = 0;
            chk = 1;
            i++;
        }
        else
        {
            mul = mul*2;
            chk++;
        }
        binaryNum = binaryNum/10;
    }
    if(chk!=1)
        hexDecNum[i] = hex+48;
    if(chk==1)
        i--;
    cout<<"\nEquivalent Value in Hexadecimal: ";
    for(i=i; i>=0; i--)
        cout<<hexDecNum[i];
    cout<<endl;
    return 0;
}

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

C++ program to convert binary to hexadecimal

Now supply the binary number input, say 1101110, and press the ENTER key to convert it to its equivalent hexadecimal value as shown in the snapshot given below:

binary to hexadecimal c++

Here is another sample run with user input as 1010:

binary to hexadecimal in c++

The dry run of the above program with user input 1010 goes like this:

Binary to hexadecimal conversion in C++ using a user-defined function

Here is another program that also converts any binary number entered by the user at run-time into its equivalent hexadecimal value, but using a user-defined function called BinaryToHexaDec().

#include<iostream>
using namespace std;

void BinaryToHexaDec(int bin);
int i=0;
char hexDecNum[20];
int main()
{
    int binaryNum;
    cout<<"Enter any Binary Number: ";
    cin>>binaryNum;
    BinaryToHexaDec(binaryNum);
    cout<<"\nEquivalent Value in Hexadecimal: ";
    for(i=i; i>=0; i--)
        cout<<hexDecNum[i];
    cout<<endl;
    return 0;
}
void BinaryToHexaDec(int bin)
{
    int hex=0, mul=1, chk=1, rem;
    while(bin!=0)
    {
        rem = bin%10;
        hex = hex + (rem*mul);
        if(chk%4==0)
        {
            if(hex<10)
                hexDecNum[i] = hex+48;
            else
                hexDecNum[i] = hex+55;
            mul = 1;
            hex = 0;
            chk = 1;
            i++;
        }
        else
        {
            mul = mul*2;
            chk++;
        }
        bin = bin/10;
    }
    if(chk!=1)
        hexDecNum[i] = hex+48;
    if(chk==1)
        i--;
}

This program will produce the same output as the previous program.

The same program in different languages

C++ Quiz


« Previous Program Next Program »