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

Basic C programming, If else, While loop, For loop, Nested loop, Array

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

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.

- Group all binary bits to 4 digits starting from right side.
- Write corresponding hexadecimal value of each grouped digit.

### Binary to hexadecimal conversion table

Decimal | Binary | Hexadecimal |
---|---|---|

0 | 0000 | 0 |

1 | 0001 | 1 |

2 | 0010 | 2 |

3 | 0011 | 3 |

4 | 0100 | 4 |

5 | 0101 | 5 |

6 | 0110 | 6 |

7 | 0111 | 7 |

8 | 1000 | 8 |

9 | 1001 | 9 |

10 | 1010 | A |

11 | 1011 | B |

12 | 1100 | C |

13 | 1101 | D |

14 | 1110 | E |

15 | 1111 | F |

## 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[20];
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);
printf("Hexadecimal number = %s", hex);
return 0;
}
```

Output

Enter binary number: 01101110 Binary number = 1101110 Hexadecimal number = 6E

Happy coding 😉

### Recommended posts

- Loop programming exercises and solutions in C.
- C program to find one's complement of a binary number.
- C program to convert Binary to Octal number system.
- C program to convert Binary to Decimal number system.
- C program to convert Hexadecimal to Binary number system.
- C program to convert Hexadecimal to Octal number system.
- C program to convert Hexadecimal to Decimal number system.

doubt, write here. I willhelp my best.<pre><code> ----Your Source Code---- </code></pre>