Hexadecimal to Decimal Conversion in C

Hexadecimal to Decimal Conversion

Hexadecimal to Decimal conversion is the process of converting a hexadecimal number (base-16) into its equivalent decimal number (base-10). Each hexadecimal digit represents a power of 16.

For example, the hexadecimal number 1F is equal to decimal 31 because:

(1 × 16¹) + (F × 16⁰) = 16 + 15 = 31

We will explore three methods to convert a hexadecimal number to a decimal number using C programming.

Method 1: Using Loop

We extract each digit of the hexadecimal number, multiply it by the corresponding power of 16, and sum the results.

#include <stdio.h>
#include <math.h>
#include <string.h>

int hexToDecimal(char hex[]) {
    int length = strlen(hex);
    int decimal = 0, base = 1;
    for (int i = length - 1; i >= 0; i--) {
        if (hex[i] >= '0' && hex[i] <= '9')
            decimal += (hex[i] - '0') * base;
        else if (hex[i] >= 'A' && hex[i] <= 'F')
            decimal += (hex[i] - 'A' + 10) * base;
        base *= 16;
    }
    return decimal;
}

int main() {
    char hex[20];
    printf("Enter a hexadecimal number: ");
    scanf("%s", hex);
    printf("Decimal equivalent: %d", hexToDecimal(hex));
    return 0;
}
            

Output:

Enter a hexadecimal number: 1F
Decimal equivalent: 31

Method 2: Using Built-in Function

We can use sscanf() to directly convert a hexadecimal number to a decimal number.

#include <stdio.h>

int main() {
    int decimal;
    char hex[20];
    printf("Enter a hexadecimal number: ");
    scanf("%s", hex);
    sscanf(hex, "%x", &decimal);
    printf("Decimal equivalent: %d", decimal);
    return 0;
}
            

Output:

Enter a hexadecimal number: 1F
Decimal equivalent: 31

Method 3: Using Recursion

We recursively extract each digit and multiply it by the corresponding power of 16.

#include <stdio.h>
#include <math.h>
#include <string.h>

int hexToDecimalRecursive(char hex[], int length, int index) {
    if (index == length)
        return 0;
    int value;
    if (hex[index] >= '0' && hex[index] <= '9')
        value = hex[index] - '0';
    else if (hex[index] >= 'A' && hex[index] <= 'F')
        value = hex[index] - 'A' + 10;
    return value * pow(16, length - index - 1) + hexToDecimalRecursive(hex, length, index + 1);
}

int main() {
    char hex[20];
    printf("Enter a hexadecimal number: ");
    scanf("%s", hex);
    printf("Decimal equivalent: %d", hexToDecimalRecursive(hex, strlen(hex), 0));
    return 0;
}
            

Output:

Enter a hexadecimal number: 1F
Decimal equivalent: 31
Numbers

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

HCF - Highest Common Factor: C C++ Java Python

LCM - Lowest Common Multiple: C C++ Java Python

GCD - Greatest Common Divisor: C C++ Java Python

Binary to Decimal Conversion: C C++ Java Python

Octal to Decimal Conversion: C C++ Java Python

Hexadecimal to Decimal Conversion: C C++ Java Python

Decimal to Binary Conversion: C C++ Java Python

Decimal to Octal Conversion: C C++ Java Python

Decimal to Hexadecimal Conversion: C C++ Java Python

Binary to Octal Conversion: C C++ Java Python

Quadrants in which a given coordinate lies: C C++ Java Python

Addition of Two Fractions: C C++ Java Python

Calculate the Area of a Circle: C C++ Java Python

Convert Digit/Number to Words: C C++ Java Python

Finding Roots of a Quadratic Equation: C C++ Java Python