Binary numbers are base-2 numbers, using only digits 0
and 1
, while hexadecimal numbers are base-16, using digits 0–9
and letters A–F
. Converting binary to hexadecimal is useful in programming, especially for memory addressing, color codes, and low-level system tasks. In this article, we will write C programs to convert binary numbers into hexadecimal format.
Understanding The Problem
Each hexadecimal digit corresponds to exactly four binary digits. To convert binary to hexadecimal, the binary number is divided into groups of four bits starting from the right. Each group is then converted into its decimal equivalent and represented as a hexadecimal digit. For example, binary 101111
can be padded as 00101111
, which converts to hexadecimal 2F
.
Program 1: Using Arithmetic Method
This method first converts the binary number into decimal and then converts the decimal into hexadecimal.
#include <stdio.h>
#include <math.h>
int main() {
long long binary;
int decimal = 0, i = 0, rem;
char hex[32];
printf("Enter a binary number: ");
scanf("%lld", &binary);
// Convert binary to decimal
while (binary > 0) {
rem = binary % 10;
decimal += rem * pow(2, i);
binary /= 10;
i++;
}
// Convert decimal to hexadecimal using sprintf
sprintf(hex, "%X", decimal);
printf("Hexadecimal equivalent: %s\n", hex);
return 0;
}
This program converts the binary input to decimal using powers of two, then uses sprintf()
with %X
to produce the hexadecimal equivalent.
Program 2: Using Strings
This method processes the binary number as a string, pads it to a multiple of four digits, and converts each group into hexadecimal.
#include <stdio.h>
#include <string.h>
int main() {
char binary[65];
int len, i;
printf("Enter a binary number: ");
scanf("%s", binary);
len = strlen(binary);
int pad = (4 - len % 4) % 4;
char padded[68] = {0};
for (i = 0; i < pad; i++) {
padded[i] = '0';
}
strcpy(padded + pad, binary);
printf("Hexadecimal equivalent: ");
for (i = 0; i < strlen(padded); i += 4) {
int value = (padded[i]-'0')*8 + (padded[i+1]-'0')*4 +
(padded[i+2]-'0')*2 + (padded[i+3]-'0')*1;
if (value < 10) printf("%d", value);
else printf("%c", value-10+'A');
}
printf("\n");
return 0;
}
This string-based approach handles large binary numbers safely by mapping each 4-bit group directly to a hexadecimal digit.
Program 3: Using strtol()
The strtol()
function can convert a binary string directly into a decimal integer, which can then be formatted as hexadecimal using sprintf()
. This method is concise, safe for large numbers, and avoids manual arithmetic.
#include <stdio.h>
#include <stdlib.h>
int main() {
char binary[65], hex[65];
unsigned long decimal;
printf("Enter a binary number: ");
scanf("%s", binary);
// Convert binary string to decimal using strtol with base 2
decimal = strtol(binary, NULL, 2);
// Convert decimal to hexadecimal string
sprintf(hex, "%lX", decimal);
printf("Hexadecimal equivalent: %s\n", hex);
return 0;
}
This program first converts the binary string to a decimal integer using strtol()
with base 2
. Then it formats the decimal as hexadecimal using sprintf()
, providing a safe and concise conversion method suitable for long binary inputs.
FAQs
Q1: Can this program handle very large binary numbers?
Yes, the string-based and sprintf()
/sscanf()
methods are safe for large inputs, while arithmetic may overflow for very long binary numbers.
Q2: Why group binary digits in fours?
Each hexadecimal digit represents exactly four binary digits ($2^4 = 16$).
Q3: Which method is easiest for beginners?
The sprintf()
is concise and avoid manual calculations, while the string-based approach is safe for long binary sequences.
Conclusion
Converting binary numbers to hexadecimal is an essential skill in programming, especially for memory management and low-level operations. Arithmetic, string-based, sprintf()
, and sscanf()
methods all provide reliable solutions. Mastering these techniques improves understanding of number systems and data representation.
References & Additional Resources
- C Programming Tutorial – Beginner-friendly guide to loops, strings, and conversions.
- GeeksforGeeks – Binary to Hexadecimal – Multiple methods for binary to hexadecimal conversion.
- TutorialsPoint – Number Systems – Complete guide to binary, octal, decimal, and hexadecimal.