Convert Binary to Hexadecimal in C
Advertisements
Convert Binary to Hexadecimal Program in C
In this types of program we takes a binary number as input and converts it into Hexadecimal number.
Binary NumberBinary 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 NumberHexadecimal number system is base 16 number system. Hexadecimal number system uses 16 symbols to represent all its values i.e. 1B
Binary to Hexadecimal Covertion Method
- Take a binary number as input.
- Group all binary bits to 4 digits starting from right side.
- Write corresponding hexadecimal value of each grouped digit.
Decimal, Binary, Hexadecimal 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 |
Convert Binary to Hexadecimal in C
#include<stdio.h> #include<conio.h> void main() { long int binary_number, hexadecimal_number = 0, i = 1, remainder; clrscr(); printf("Please Enter any Binary Number: "); scanf("%ld", &binary_number); while (binary_number != 0) { remainder = binary_number % 10; hexadecimal_number = hexadecimal_number + remainder * i; i = i * 2; binary_number = binary_number / 10; } printf("Equivalent Hexadecimal Number %lX", hexadecimal_number); getch(); }
Output 1
Please Enter any Binary Number: 10011 Equivalent Hexadecimal Number: 13
Output 2
Please Enter any Binary Number: 11011 Equivalent Hexadecimal Number: 1B
Google Advertisment