# C program to convert Binary to Hexadecimal number system

Write a C program to input binary number from user and convert to hexadecimal number system. How to convert binary to hexadecimal number system in C program. Logic to convert binary to hexadecimal number system in C programming.

Example

Input

`Enter binary number: 11000010`

Output

`Hexadecimal: C2`

## Required knowledge

Must know - Program to find last digit

### Binary number system

Binary number system is a base 2 number system. Binary number system uses only two symbols to represent all its values i.e. `0` and `1`

Hexadecimal number system is base 16 number system. Hexadecimal number system uses 16 symbols to represent all its values i.e. `0123456789ABCDEF`

## Logic to convert binary to hexadecimal number system

Binary to hexadecimal conversion algorithm is divided in two parts.

1. Group all binary bits to 4 digits starting from right side.
2. Write corresponding hexadecimal value of each grouped digit. 000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F

## Program to convert binary to hexadecimal number system

``````/**
* C program to convert binary to hexadecimal number system
*/

#include <stdio.h>
#include <string.h>

int main()
{
int hexConstant[] = {0, 1, 10, 11, 100, 101, 110, 111, 1000,
1001, 1010, 1011, 1100, 1101, 1110, 1111};

long long binary, tempBinary;
char hex;
int index, i, digit;

/* Input binary number from user */
printf("Enter binary number: ");
scanf("%lld", &binary);

/* Copy binary number to temp variable */
tempBinary = binary;
index = 0;

/* Find hexadecimal of binary number */
while(tempBinary!=0)
{
/* Group binary to last four digits */
digit = tempBinary % 10000;

/* Find hexadecimal equivalent of last four digit */
for(i=0; i<16; i++)
{
if(hexConstant[i] == digit)
{
if(i<10)
{
/* 0-9 integer constant */
hex[index] = (char)(i + 48);
}
else
{
/* A-F character constant */
hex[index] = (char)((i-10) + 65);
}

index++;
break;
}
}

/* Remove the last 4 digits as it is processed */
tempBinary /= 10000;
}
hex[index] = '\0';

/* Reverse the hex digits */
strrev(hex);

printf("Binary number = %lld\n", binary);

return 0;
}``````

Output

```Enter binary number: 01101110
Binary number = 1101110 