Hexadecimal to Binary in C
What is hexadecimal?
The hexadecimal is a positional system that represents the numbers in sixteen distinct symbols by using 16 as a base. These distinct symbols, i.e., “0-9” represents the values from zero to nine, and “A-F” represents the values from ten to fifteen.
What is Binary?
The binary is a number which is represented with a base 2 numeral system, and it uses two symbols, i.e., 0 and 1.
Examples
Suppose the hexadecimal number is A12C. Now we calculate the binary value of the hexadecimal number.
Hexadecimal number=A12C
Binary value equivalent to A is 1010
Binary value equivalent to 1 is 0001
Binary value equivalent to 2 is 0010
Binary value equivalent to C is 1100
Therefore, the binary value equivalent to A12C is 1010000100101100.
Note: To convert hexadecimal into a binary number, the binary value of each digit of a hexadecimal number is evaluated and combined to get the binary number of a given hexadecimal number.
Let’s understand through an example.
In the above code, we want to calculate the binary value of “A12C“. We first store this value in a character array named as hex[], and then we pass this array to a hextobin() function. The hextobin() will calculate the binary value of the number, it iterates over each element of an array through a while loop, and finds the binary value of each corresponding element through a switch statement.
Output
1010000100101100