codescracker
c++

C++ Program to Convert Binary to Hexadecimal



« Previous Program Next Program »

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

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

Binary to Hexadecimal in C++

To convert binary number to hexadecimal in C++ programming, you have to ask from user to enter any number in 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 prints 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 build and run under 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 ENTER key 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 above program) with user input 1010 goes like:

Using user-defined Function

Here is another program that also converts any binary number entered by user at run-time into its equivalent hexadecimal value, but using a user-defined function 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 of previous program.

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker