Hexadecimal numbers are base-16, represented using digits 0–9
and letters A–F
. Binary numbers use base-2 with digits 0
and 1
. Converting hexadecimal to binary is useful in programming for memory addressing, color codes, and low-level system operations. In this article, we will write C programs to convert hexadecimal numbers into binary format.
Understanding The Problem
Each hexadecimal digit corresponds exactly to four binary digits because $2^4 = 16$. To convert hexadecimal to binary, each digit is converted individually into a 4-bit binary group, and these groups are concatenated to form the full binary number. For example, hexadecimal 2F
converts to binary 00101111
.
Program 1: Using Arithmetic Method
This approach converts the hexadecimal number into decimal first and then converts the decimal number into binary.
#include <stdio.h>
#include <math.h>
#include <string.h>
int main() {
char hex[20];
int decimal = 0, i, rem, len;
long long binary = 0, factor = 1;
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
// Convert hexadecimal to decimal
len = strlen(hex);
for (i = 0; i < len; i++) {
if (hex[i] >= '0' && hex[i] <= '9')
rem = hex[i] - '0';
else if (hex[i] >= 'A' && hex[i] <= 'F')
rem = hex[i] - 'A' + 10;
else if (hex[i] >= 'a' && hex[i] <= 'f')
rem = hex[i] - 'a' + 10;
else {
printf("Invalid hexadecimal digit\n");
return 1;
}
decimal += rem * pow(16, len - i - 1);
}
// Convert decimal to binary
int temp = decimal;
while (temp > 0) {
rem = temp % 2;
binary += rem * factor;
temp /= 2;
factor *= 10;
}
printf("Binary equivalent: %lld\n", binary);
return 0;
}
This program first converts the hexadecimal input into decimal using powers of 16, then converts the decimal value into binary.
Program 2: Using Strings
This method maps each hexadecimal digit directly to a 4-bit binary string, avoiding decimal conversion and allowing safe handling of long inputs.
#include <stdio.h>
#include <string.h>
int main() {
char hex[20];
int i;
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
printf("Binary equivalent: ");
for (i = 0; i < strlen(hex); i++) {
switch(hex[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("Invalid hex digit"); return 1;
}
}
printf("\n");
return 0;
}
This string-based approach converts each hexadecimal character into its corresponding 4-bit binary string and concatenates them.
Program 3: Using sprintf()
and Decimal Conversion
After converting hexadecimal to decimal, we can use sprintf()
to produce the binary representation.
#include <stdio.h>
#include <stdlib.h>
int main() {
char hex[20], binary[65];
unsigned int decimal;
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
// Convert hexadecimal string to decimal
decimal = (unsigned int) strtol(hex, NULL, 16);
// Convert decimal to binary string using sprintf (C23 %b)
sprintf(binary, "%b", decimal);
printf("Binary equivalent: %s\n", binary);
return 0;
}
This method is concise and uses C23’s %b
format specifier in sprintf()
to directly get the binary representation.
Program 4: Using sscanf()
(C23)
In C23, sscanf()
can read hexadecimal input and then binary output is printed using %b
.
#include <stdio.h>
int main() {
char hex[20];
unsigned int decimal;
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
// Convert hex string to decimal using sscanf and %x
sscanf(hex, "%x", &decimal);
// Print binary using %b (C23)
printf("Binary equivalent: %b\n", decimal);
return 0;
}
This program reads the hexadecimal string as decimal with %x
and prints its binary using %b
. It is very concise and modern but requires a C23-compliant compiler.
Program 5: Using strtol()
The strtol()
function can convert a hexadecimal string directly into a decimal integer, which can then be converted into binary. This approach is concise and safe for large numbers.
#include <stdio.h>
#include <stdlib.h>
int main() {
char hex[20], binary[65];
unsigned long decimal;
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
// Convert hexadecimal string to decimal using strtol
decimal = strtol(hex, NULL, 16);
// Convert decimal to binary manually
if (decimal == 0) {
printf("Binary equivalent: 0\n");
return 0;
}
int index = 0;
unsigned long temp = decimal;
while (temp > 0) {
binary[index++] = (temp % 2) + '0';
temp /= 2;
}
// Reverse the string to get correct binary representation
printf("Binary equivalent: ");
for (int i = index - 1; i >= 0; i--) {
putchar(binary[i]);
}
printf("\n");
return 0;
}
This program first uses strtol()
to safely convert the hexadecimal input into a decimal number. Then it builds the binary representation manually by repeatedly dividing by 2 and collecting remainders, reversing the result at the end.
FAQs
Q1: Can this program handle lowercase hex letters?
Yes, both the string-based and sscanf()
methods handle lowercase a–f
.
Q2: Why does each hex digit map to 4 binary digits?
Because $2^4 = 16$, each hexadecimal digit exactly corresponds to four binary digits.
Q3: Which method is easiest for beginners?
The string-based mapping is simple and safe, while sprintf()
or sscanf()
provides a concise modern approach for C23 compilers.
Conclusion
Converting hexadecimal numbers to binary is straightforward using arithmetic, string mapping, sprintf()
, or sscanf()
. Understanding this conversion is important for memory representation, low-level operations, and system programming.
References & Additional Resources
- C Programming Tutorial – Covers loops, strings, and conversions in C.
- GeeksforGeeks – Hexadecimal to Binary – Examples of multiple conversion methods.
- TutorialsPoint – Number Systems – Complete guide to binary, octal, decimal, and hexadecimal.