Articles

C++ program to convert binary to hexadecimal

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.

c++ program to convert binary to hexadecimal

c++ program to convert binary to hexadecimal

WORKING OF BINARY TO HEXADECIMAL CONVERTER PROGRAM :

c++ program to convert binary to hexadecimal

 


CPP Programs

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  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!