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;
}
cout<<"Equivalent hexadecimal value: "<<hexa_val;
}
//Main function
int main()
{
long int num;
cout<<"Enter the binary number: ";
cin>>num;
solve(num);
return 0;
}
OUTPUT
Enter the binary number
001
Equivalent hexadecimal value:
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 :
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Sayantan Bose
- š Iām currently working on DS Algo skills
- š± Iām currently learning web develeopement
- šÆ Iām looking to collaborate with Oppia
- š« Reach me at https://www.linkedin.com/in/sayantan-bose-14134a1a6/
- š Pronouns: his/him
Page Views :
Published Date :
Jan 21,2021