Hexadecimal numbers are base-16 numbers using digits 0–9
and letters A–F
. Converting hexadecimal to decimal is a common requirement in programming, especially in memory addressing, color codes, and low-level system tasks. This article shows how to write a C program to convert a hexadecimal number into decimal format.
Understanding The Problem
Decimal numbers use base-10, while hexadecimal uses base-16. To convert a hexadecimal number to decimal, each digit is multiplied by 16
raised to the power of its position, starting from the rightmost digit (Least Significant Digit). For example, the hexadecimal number 1F
is calculated as: 1*16^1 + 15*16^0 = 31
. Our program should read a hexadecimal number and compute its decimal equivalent using this logic.
Program 1: Using sscanf()
and Integer Conversion
In this method, the hexadecimal string is read as input, and sscanf()
is used to convert it into a decimal integer directly.
#include <stdio.h>
int main() {
char hex[20];
int decimal;
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
sscanf(hex, "%x", &decimal); // %x reads hexadecimal input
printf("Decimal equivalent: %d\n", decimal);
return 0;
}
This program reads the hexadecimal input as a string and converts it directly to decimal using sscanf()
. It is simple and efficient for standard C compilers.
Program 2: Manual Conversion Using Loops
This method processes each hexadecimal digit individually, multiplies it by powers of 16, and accumulates the result.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main() {
char hex[20];
int decimal = 0, len, i;
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
len = strlen(hex);
for (i = 0; i < len; i++) {
char c = toupper(hex[i]);
int value;
if (c >= '0' && c <= '9')
value = c - '0';
else
value = c - 'A' + 10;
decimal += value * pow(16, len - i - 1);
}
printf("Decimal equivalent: %d\n", decimal);
return 0;
}
This approach demonstrates the underlying logic of hexadecimal-to-decimal conversion. Each character is converted into a numeric value, multiplied by 16^position
, and added to the total.
Program 3: Using strtol()
strtol()
can convert a hexadecimal string directly into a decimal integer by specifying base 16. This method is simple, safe, and works for large numbers.
#include <stdio.h>
#include <stdlib.h>
int main() {
char hex[20];
long decimal;
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
// Convert hexadecimal string to decimal using strtol
decimal = strtol(hex, NULL, 16);
printf("Decimal equivalent: %ld\n", decimal);
return 0;
}
This method reads the hexadecimal input as a string, converts it into a decimal integer using strtol()
with base 16
, and prints the result. It handles both uppercase and lowercase letters and is robust for large inputs.
FAQs
Q1: Can this program handle lowercase hexadecimal letters?
Yes, the manual method converts letters to uppercase before processing, so both a–f
and A–F
are accepted.
Q2: Which method is simpler for beginners?
Using sscanf()
is simpler and less error-prone. The manual method is more educational and shows how conversions work internally.
Q3: Can negative numbers be entered in hexadecimal?
Yes, hexadecimal integers can represent negative numbers using two’s complement notation, but additional handling is required for proper input.
Conclusion
Converting hexadecimal to decimal is an essential skill in programming. Both sscanf()
and manual processing approaches provide accurate results. Understanding both methods gives insight into number system conversions and low-level data representation.
References & Additional Resources
- C Programming Tutorial – Covers format specifiers and input/output in C.
- GeeksforGeeks – Hexadecimal to Decimal – Multiple methods explained for conversion.
- TutorialsPoint – C Data Types – Understanding integer representation and limits.
- TutorialsPoint – Number Systems – Complete guide to binary, octal, decimal, and hexadecimal.