RUN

# Convert Binary to Hexadecimal program in C++

Written By -

Binary Number: A binary number is a number expressed in the base-2 binary numeral system, using only two symbols: which are 0 (zero) and 1 (one).

HexaDecimal Number: A hexadecimal number is a positional numeral system with base 16 and uses sixteen distinct symbols (0 to 15).

Example:

Binary number: 10110

Binary number: 101010

### # Algorithm

1. Take a binary number as input.
2. Group all binary bits to 4 digits (starting from the right side).
3. Write the corresponding hexadecimal value of each grouped digit.

Code:

``````#include <iostream>
using namespace std;

int main()

{

char bno[1000], hex[1000];

int temp;

long int i = 0, j = 0;

cout << "Enter Binary Number : ";

cin >> bno;

while (bno[i])

{

bno[i] = bno[i] - 48;

++i;
}

--i;

while (i - 2 >= 0)

{

temp = bno[i - 3] *8 + bno[i - 2] *4 + bno[i - 1] *2 + bno[i];

if (temp > 9)

hex[j++] = temp + 55;

else

hex[j++] = temp + 48;

i = i - 4;
}

if (i == 1)

hex[j] = bno[i - 1] *2 + bno[i] + 48;

else if (i == 0)

hex[j] = bno[i] + 48;

else

--j;

cout << "\nHexadecimal Number equivalent to Binary Number : ";

while (j >= 0)

{

cout << hex[j--];
}

return 0;

}``````