In this tutorial you will learn about the C Program to Convert Hexadecimal to Binary and its application with practical example.
C Program to Convert Hexadecimal to Binary
In this tutorial, we will learn to create a C program that will Convert Hexadecimal to Binary in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
Program to Convert Hexadecimal to Binary:-
As we all know the c is a very powerful language. With the help of c programming language, we can make many programs. We cal perform many input-output operations using c programming. In today’s tutorial, we take the input in hexadecimal from the user and convert it into binary. With the help of c programming, we can perform many conversion operations.
With the help of this program, we can Convert Hexadecimal to Binary.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declare the variables for the program. 2. Takeing the input number from in hexa for the program. 3. Passing that input to functions. 4. Print the Result. 5. End the program. |
Program to Convert Hexadecimal to Binary:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
/* C Program to Convert Hexadecimal to Binary */ #include <stdio.h> // function to convert Hexadecimal to Binary Number void HexToBin(char* hexdec) { long int i = 0; while (hexdec[i]) { switch (hexdec[i]) { case '0': printf("0000"); break; case '1': printf("0001"); break; case '2': printf("0010"); break; case '3': printf("0011"); break; case '4': printf("0100"); break; case '5': printf("0101"); break; case '6': printf("0110"); break; case '7': printf("0111"); break; case '8': printf("1000"); break; case '9': printf("1001"); break; case 'A': case 'a': printf("1010"); break; case 'B': case 'b': printf("1011"); break; case 'C': case 'c': printf("1100"); break; case 'D': case 'd': printf("1101"); break; case 'E': case 'e': printf("1110"); break; case 'F': case 'f': printf("1111"); break; default: printf("\nInvalid hexadecimal digit %c", hexdec[i]); } i++; } } // main fucntion code int main() { // Get the Hexadecimal number char hexdec[100] = "1AC5"; // Convert HexaDecimal to Binary printf("\nEquivalent Binary value is : "); HexToBin(hexdec); } |
Output:-
In the above program, we have first initialized the required variable.
- hex[100] = it will hold the Hexa value for the program.
Hexa to binary
Main function.