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

Input binary number: (1100 0010)

Output hexadecimal number: (C2)

Step 1: Break the binary number in the group of 4 digits from right side.

Step 2: Write the corresponding hexadecimal equivalent value in place of grouped binary value.

Since hex uses group of four binary bits hence you can use the below table for conversion

Happy coding ;)

**Example:**Input binary number: (1100 0010)

_{2}Output hexadecimal number: (C2)

_{16}### Required knowledge:

Basic C programming, For loop#### 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 a base 16 number system. Hexadecimal number system uses 16 symbols to represent all its values i.e. 0 1 2 3 4 5 6 7 8 9 A B C D E F### Algorithm to convert Binary to Hexadecimal number system:

Conversion from binary to hexadecimal number system is easy and simple. What you need to do isStep 1: Break the binary number in the group of 4 digits from right side.

Step 2: Write the corresponding hexadecimal equivalent value in place of grouped binary value.

Since hex uses group of four binary bits hence you can use the below table for conversion

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:

/** * 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; /* * Reads binary number from user */ printf("Enter binary number: "); scanf("%lld", &binary); /* Copies binary number to a temp variable */ tempBinary = binary; index = 0; /* * Finds the hexadecimal of binary number */ while(tempBinary!=0) { digit = tempBinary % 10000; 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; } } tempBinary /= 10000; } hex[index] = '\0'; strrev(hex); printf("\nOriginal binary number = %lld\n", binary); printf("Hexadecimal number = %s", hex); return 0; }

Output

Enter binary number: 01101110

Original binary number = 1101110

Hexadecimal number = 6E

Original binary number = 1101110

Hexadecimal number = 6E

Happy coding ;)

### You may also like

- Loop programming exercises and solutions in C.
- C program to find one's complement of a binary number.
- C program to find two'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 Octal to Binary number system.
- C program to convert Octal to Decimal number system.
- C program to convert Octal to Hexadecimal number system.
- C program to convert Decimal to Binary number system.
- C program to convert Decimal to Octal number system.
- C program to convert Decimal to Hexadecimal 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.

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).