Articles

# C++ program to convert binary to hexadecimal

In this chapter of C++ program tutorial out task is to write:

• binary to hexadecimal conversion in c++

### Binary to hexadecimal conversion in C++

Below is the program in c++ program to convert binary to hexadecimal using functions

``````/*  Program Name: To convert a binary number to its hexadecimal equivalent
Program Author: Sayantan Bose
*/
#include <iostream>
using namespace std;

//Function to convert binary number to its hexadecimal equivalent
void solve(int num)
{
long int hexa_val = 0, i = 1, r;
while (num != 0)
{
r = num % 10;
hexa_val = hexa_val + r * i;
i = i * 2;
num = num / 10;
}

}

//Main function
int main()
{
long int num;

cout<<"Enter the binary number: ";
cin>>num;
solve(num);
return 0;
}
``````

OUTPUT

Enter the binary number

001

1

EXPLANATION :
In the above c++ program to convert binary to hexadecimal using function:
• A binary number can be converted to its hexadecimal equivalent by grouping 4 digits from the left side of the number, and converting the 4 digit number to their hexadecimal equivalent. We can follow the below tables for the same.
• In the above program, a given binary number is passed on to the function solve where it is converted to its hexadecimal equivalent by the same logic.  WORKING OF BINARY TO HEXADECIMAL CONVERTER PROGRAM : CPP Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com 