Hexadecimal numbers are base-16 numbers represented using digits 0–9
and letters A–F
. Converting from decimal to hexadecimal is a common task in programming, particularly in memory addressing, graphics (like color codes), and low-level development. This section explains how to write a C program to perform the conversion.
Understanding The Problem
Decimal numbers use base-10, while hexadecimal uses base-16. To convert a decimal number into hexadecimal, we repeatedly divide the number by 16 and note the remainders. These remainders, when reversed, form the hexadecimal number. For example, the decimal number 255
becomes FF
in hexadecimal.
Program 1: Using Loops and Arrays
This approach uses division and remainder operations. We store each remainder as a hexadecimal digit and then reverse the order to print the final hexadecimal number.
#include <stdio.h>
int main() {
int n;
char hex[100];
int i = 0;
printf("Enter a decimal number: ");
scanf("%d", &n);
if (n == 0) {
printf("Hexadecimal equivalent: 0\n");
return 0;
}
while (n > 0) {
int rem = n % 16;
if (rem < 10)
hex[i] = rem + '0'; // convert number to char
else
hex[i] = rem - 10 + 'A'; // convert 10–15 to A–F
i++;
n /= 16;
}
printf("Hexadecimal equivalent: ");
for (int j = i - 1; j >= 0; j--) {
printf("%c", hex[j]);
}
printf("\n");
return 0;
}
This program breaks down the decimal number step by step using division by 16. It collects the remainders, converts them into characters, and finally reverses them to display the correct hexadecimal result.
Program 2: Using sprintf()
Instead of manually performing the conversion, we can use C’s built-in formatting capability. The sprintf()
function allows us to directly convert a decimal integer into hexadecimal form using format specifiers.
#include <stdio.h>
int main() {
int n;
char hex[100];
printf("Enter a decimal number: ");
scanf("%d", &n);
sprintf(hex, "%X", n); // %X prints uppercase hexadecimal; %x prints lowerercase hexadecimal
printf("Hexadecimal equivalent: %s\n", hex);
return 0;
}
This method makes use of string formatting functions. It is quick, clean, and avoids manually computing remainders. The result is the same, but the process is much simpler.
FAQs
In this section, we answer common questions about decimal to hexadecimal conversion.
Q1: What is the difference between %x
and %X
in C?%x
prints hexadecimal in lowercase (a–f
), while %X
prints it in uppercase (A–F
).
Q2: Can negative numbers be converted?
Yes, but they are represented in hexadecimal using two’s complement notation, which may not be intuitive.
Q3: Which method should I use as a beginner?
Using sprintf()
is easier and less error-prone, but the manual method helps you understand how the conversion actually works.
Conclusion
Converting decimal numbers into hexadecimal is a fundamental skill for programmers. The manual method with loops helps in understanding number systems, while the sprintf()
method demonstrates how built-in tools can simplify the task. Both are important and useful depending on the context.
References & Additional Resources
Here are some useful resources to understand decimal-to-hexadecimal conversion in C.
- C Programming Tutorial – Covers number systems and format specifiers.
- GeeksforGeeks – Decimal to Hexadecimal – Explains different methods of conversion.
- TutorialsPoint – C Input and Output – Guide to using
printf()
andsprintf()
effectively. - TutorialsPoint – Number Systems – A complete guide to binary, octal, decimal, and hexadecimal systems.