0
0
CProgramBeginner · 2 min read

C Program to Convert Hexadecimal to Decimal Number

Use the C function strtol(hexString, NULL, 16) to convert a hexadecimal string to decimal integer; for example, int decimal = (int)strtol("1A", NULL, 16); converts hex "1A" to decimal 26.
📋

Examples

Input1A
OutputDecimal value: 26
InputFF
OutputDecimal value: 255
Input0
OutputDecimal value: 0
🧠

How to Think About It

To convert a hexadecimal number to decimal, read the hex string as input, then interpret each digit according to base 16. Multiply each digit by 16 raised to the power of its position index from right to left, and sum all these values to get the decimal equivalent.
📐

Algorithm

1
Get the hexadecimal number as a string input.
2
Initialize a variable to store the decimal result as 0.
3
For each character in the hex string from left to right:
4
Convert the hex digit to its decimal value (0-15).
5
Multiply the current decimal result by 16 and add the digit value.
6
After processing all digits, return or print the decimal result.
💻

Code

c
#include <stdio.h>
#include <stdlib.h>

int main() {
    char hex[20];
    printf("Enter a hexadecimal number: ");
    scanf("%19s", hex);
    int decimal = (int)strtol(hex, NULL, 16);
    printf("Decimal value: %d\n", decimal);
    return 0;
}
Output
Enter a hexadecimal number: 1A Decimal value: 26
🔍

Dry Run

Let's trace input '1A' through the code

1

Input read

hex = "1A"

2

Convert hex to decimal

strtol("1A", NULL, 16) returns 26

3

Print result

Prints "Decimal value: 26"

Hex DigitDecimal ValueRunning Total
111
A101*16 + 10 = 26
💡

Why This Works

Step 1: Reading input as string

The program reads the hexadecimal number as a string to handle digits and letters.

Step 2: Using strtol for conversion

The strtol function converts the hex string to a decimal integer by interpreting base 16.

Step 3: Displaying the decimal value

The program prints the decimal integer, showing the converted value.

🔄

Alternative Approaches

Manual conversion by iterating digits
c
#include <stdio.h>
#include <string.h>

int hexCharToDecimal(char c) {
    if (c >= '0' && c <= '9') return c - '0';
    if (c >= 'A' && c <= 'F') return c - 'A' + 10;
    if (c >= 'a' && c <= 'f') return c - 'a' + 10;
    return -1; // invalid
}

int main() {
    char hex[20];
    printf("Enter a hexadecimal number: ");
    scanf("%19s", hex);
    int decimal = 0;
    for (int i = 0; i < (int)strlen(hex); i++) {
        int val = hexCharToDecimal(hex[i]);
        if (val == -1) {
            printf("Invalid hex digit\n");
            return 1;
        }
        decimal = decimal * 16 + val;
    }
    printf("Decimal value: %d\n", decimal);
    return 0;
}
This method manually processes each digit, useful for learning but more code and error-prone.
Using sscanf with %x format specifier
c
#include <stdio.h>

int main() {
    char hex[20];
    int decimal;
    printf("Enter a hexadecimal number: ");
    scanf("%19s", hex);
    sscanf(hex, "%x", &decimal);
    printf("Decimal value: %d\n", decimal);
    return 0;
}
This uses <code>sscanf</code> to parse hex directly, simpler but less flexible than <code>strtol</code>.

Complexity: O(n) time, O(1) space

Time Complexity

The program processes each character of the hex string once, so time grows linearly with input length.

Space Complexity

Only a few variables are used, so space is constant regardless of input size.

Which Approach is Fastest?

Using strtol or sscanf is fastest and simplest; manual conversion is educational but slower.

ApproachTimeSpaceBest For
strtol functionO(n)O(1)Simple, robust conversion
Manual digit processingO(n)O(1)Learning and custom validation
sscanf with %xO(n)O(1)Quick parsing with less code
💡
Use strtol with base 16 for easy and safe hex to decimal conversion in C.
⚠️
Forgetting to specify base 16 causes wrong conversion or zero result.