How to make a decimal to hexadecimal converter in C
Categories:
Converting Decimal to Hexadecimal in C: A Comprehensive Guide

Learn how to implement a decimal to hexadecimal converter in C using various methods, including custom logic and standard library functions. This guide covers the underlying principles and provides practical code examples.
Converting numbers between different bases is a fundamental concept in computer science. While decimal (base-10) is our everyday system, hexadecimal (base-16) is widely used in programming for representing memory addresses, color codes, and byte values due to its compact representation. This article will guide you through creating a decimal to hexadecimal converter in C, exploring both manual conversion logic and leveraging built-in functions.
Understanding Decimal to Hexadecimal Conversion
The core principle behind converting a decimal number to any other base is repeated division by the target base. For hexadecimal, this means repeatedly dividing the decimal number by 16 and recording the remainders. The remainders, read in reverse order, form the hexadecimal representation. Since hexadecimal uses digits 0-9 and letters A-F for values 10-15, we need a mapping for these remainders.
flowchart TD A[Start with Decimal Number] --> B{Divide by 16} B --> C[Record Remainder] C --> D{Is Quotient 0?} D -- No --> B D -- Yes --> E[Map Remainders (0-9, A-F)] E --> F[Read Mapped Remainders in Reverse Order] F --> G[End: Hexadecimal Result]
Flowchart of the Decimal to Hexadecimal Conversion Algorithm
Method 1: Manual Conversion Logic
This method involves implementing the repeated division and remainder collection logic from scratch. It's excellent for understanding the underlying algorithm. We'll use an array to store the hexadecimal digits as they are calculated and then print them in reverse order.
#include <stdio.h>
void decToHexManual(int decNum) {
char hexDigits[100];
int i = 0;
if (decNum == 0) {
printf("0\n");
return;
}
while (decNum > 0) {
int remainder = decNum % 16;
if (remainder < 10) {
hexDigits[i] = remainder + '0';
} else {
hexDigits[i] = remainder + 'A' - 10;
}
decNum /= 16;
i++;
}
printf("Hexadecimal equivalent: ");
for (int j = i - 1; j >= 0; j--) {
printf("%c", hexDigits[j]);
}
printf("\n");
}
int main() {
int decimalNumber;
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);
decToHexManual(decimalNumber);
return 0;
}
C code for manual decimal to hexadecimal conversion
remainder + 'A' - 10
logic works because 'A' is ASCII 65, 'B' is 66, and so on. If the remainder is 10, 10 + 'A' - 10
evaluates to 'A'. If the remainder is 11, it evaluates to 'B', and so forth.Method 2: Using sprintf
for Simplicity
The C standard library provides powerful functions for formatted output. The sprintf
function can convert various data types into a string, and it supports format specifiers for hexadecimal output. This is often the simplest and most robust way to perform the conversion.
#include <stdio.h>
void decToHexSprintf(int decNum) {
char hexString[20]; // Sufficiently large buffer for int to hex
sprintf(hexString, "%X", decNum); // %X for uppercase hex, %x for lowercase
printf("Hexadecimal equivalent: %s\n", hexString);
}
int main() {
int decimalNumber;
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);
decToHexSprintf(decimalNumber);
return 0;
}
C code using sprintf
for decimal to hexadecimal conversion
%X
format specifier in sprintf
(and printf
) automatically handles the conversion to uppercase hexadecimal digits. Use %x
for lowercase hexadecimal.Choosing the Right Method
Both methods achieve the same goal, but they serve different purposes:
- Manual Conversion: Ideal for educational purposes, understanding algorithms, or when you need fine-grained control over the output format (e.g., custom digit mapping, specific padding). It also avoids relying on library functions if you're in a highly constrained environment.
sprintf
Method: Recommended for most practical applications due to its simplicity, robustness, and efficiency. It's less prone to errors and handles edge cases (like0
) automatically. It's part of the standard library, so it's widely available and optimized.
1. Include necessary headers
For manual conversion, stdio.h
is sufficient. For sprintf
, you also need stdio.h
.
2. Get decimal input
Use scanf
to read the integer decimal number from the user.
3. Perform conversion
Choose either the manual division-remainder logic or the sprintf
function with the %X
format specifier.
4. Display the result
Print the resulting hexadecimal string to the console.