# C Program to Convert Hexadecimal to Binary

«
»

This is a C program to Convert Hexadecimal to Binary.

Problem Description

This program takes a hexadecimal number and converts to binary number.

Problem Solution

1. Take a hexadecimal number as input.
2. For each bit of a hexadecimal number print its equivalent binary number in a four bit fashion. Example: For 22 print it as 0010 0010.
3. Use switch statement to access each bit of a hexadecimal number.

Program/Source Code

Here is source code of the C program to Convert Hexadecimal to Binary. The C program is successfully compiled and run on a Linux system. The program output is also shown
below.

1. `/*`
2. ` * C Program to Convert Hexadecimal to Binary`
3. ` */`
4. `#include <stdio.h>`
5. `#define MAX 1000`
6. ` `
7. `int main()`
8. `{`
9. `    char binarynum[MAX], hexa[MAX];`
10. `    long int i = 0;`
11. ` `
12. `    printf("Enter the value for hexadecimal ");`
13. `    scanf("%s", hexa);`
14. `    printf("\n Equivalent binary value: ");`
15. `    while (hexa[i])`
16. `    {`
17. `        switch (hexa[i])`
18. `        {`
19. `        case '0':`
20. `            printf("0000"); break;`
21. `        case '1':`
22. `            printf("0001"); break;`
23. `        case '2':`
24. `            printf("0010"); break;`
25. `        case '3':`
26. `            printf("0011"); break;`
27. `        case '4':`
28. `            printf("0100"); break;`
29. `        case '5':`
30. `            printf("0101"); break;`
31. `        case '6':`
32. `            printf("0110"); break;`
33. `        case '7':`
34. `            printf("0111"); break;`
35. `        case '8':`
36. `            printf("1000"); break;`
37. `        case '9':`
38. `            printf("1001"); break;`
39. `        case 'A':`
40. `            printf("1010"); break;`
41. `        case 'B':`
42. `            printf("1011"); break;`
43. `        case 'C':`
44. `            printf("1100"); break;`
45. `        case 'D':`
46. `            printf("1101"); break;`
47. `        case 'E':`
48. `            printf("1110"); break;`
49. `        case 'F':`
50. `            printf("1111"); break;`
51. `        case 'a':`
52. `            printf("1010"); break;`
53. `        case 'b':`
54. `            printf("1011"); break;`
55. `        case 'c':`
56. `            printf("1100"); break;`
57. `        case 'd':`
58. `            printf("1101"); break;`
59. `        case 'e':`
60. `            printf("1110"); break;`
61. `        case 'f':`
62. `            printf("1111"); break;`
63. `        default:`
64. `            printf("\n Invalid hexa digit %c ", hexa[i]);`
65. `            return 0;`
66. `        }`
67. `        i++;`
68. `    }`
69. `    return 0;`
70. `}`
Program Explanation

1. Take a hexadecimal number as input and store it in the array hexa.
2. Using switch statement access each bit of the hexadecimal number and print its equivalent binary number in a four bit fashion as shown in the program.
3. Do step 2 for every bit of a input number. Use while loop to do this.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
```Output:

Enter the value for hexadecimal ab
Equivalent binary value: 10101011```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.