In this article, we are going to learn the C program to Convert Binary to Hexadecimal numbers. Here we are going to learn about two number systems one is binary and another is Hexadecimal. Binary numbers are base 2 numbers and represented in 0s and 1s. Hexadecimal numbers are base 16 numbers and represented in numbers from 0 to 9 and A to F.
The binary to Hexadecimal equivalent number table is given below:
Decimal Number | Binary Numbers | Hexadecimal Number |
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 |
C program to convert binary to hexadecimal number
#include <stdio.h>
int main()
{
long long binNum,tempBin;
long int hexNum;
int i = 1;
int remainder;
hexNum = 0;
printf("Please enter 8 bit binary number: ");
scanf("%lld", &binNum);
tempBin = binNum;
while (binNum != 0)
{
remainder = binNum % 10;
hexNum = hexNum + remainder * i;
i = i * 2;
binNum = binNum / 10;
}
printf("HexaDecimal Equivalent to Binary number = %lld is = %lX \n", tempBin,hexNum);
return 0;
}