Sai A
Updated date Oct 20, 2023
In this blog, we will explore the various methods for converting hexadecimal numbers to decimal in C. From the basics to advanced methods, each approach is discussed and accompanied by a detailed program, output.

## Introduction:

Hexadecimal and decimal are two commonly used number systems in computer science. While hexadecimal is base-16, decimal is base-10. Converting a hexadecimal number to a decimal is a fundamental skill for programmers. In this blog, we will learn various methods of converting hex to decimal in C

## Method 1: Using Standard Library Function `strtol`:

The `strtol` function is part of the C standard library and is commonly used for converting strings to long integers. Here's a simple program utilizing `strtol` for hex to decimal conversion:

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

int main() {
char hexNumber[] = "1A";
char *endPtr;

long decimalNumber = strtol(hexNumber, &endPtr, 16);

printf("Method 1 Output: %ld\n", decimalNumber);

return 0;
}
``````

### Output:

``````Method 1 Output: 26
``````
• We declare a character array `hexNumber` containing the hexadecimal value "1A."
• The `strtol` function converts the hex value to a long integer, and the result is stored in `decimalNumber`.
• Finally, we print the decimal result using `printf`.

## Method 2: Manual Conversion with Iterative Process:

Let's implement a manual conversion by iterating through each hex digit, converting it to its decimal equivalent, and accumulating the result:

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

int hexToDecimal(char hex[]) {
int length = strlen(hex);
int decimal = 0;
int 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;
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
decimal += (hex[i] - 'a' + 10) * base;
}

base *= 16;
}

return decimal;
}

int main() {
char hexNumber[] = "1A";

int decimalNumber = hexToDecimal(hexNumber);

printf("Method 2 Output: %d\n", decimalNumber);

return 0;
}
``````

### Output:

``````Method 2 Output: 26
``````
• The function `hexToDecimal` takes a hexadecimal string as input and iterates through each digit.
• It checks whether the current digit is in the range of '0' to '9' or 'A' to 'F' (or 'a' to 'f').
• The decimal value is accumulated by considering the positional value of each digit.
• The result is printed in the `main` function.

## Method 3: Using Pow Function for Positional Value Calculation:

Another approach involves using the `pow` function from the `math.h` library to calculate positional values. This can make the code more concise:

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

int hexToDecimal(char hex[]) {
int length = strlen(hex);
int decimal = 0;

for (int i = length - 1; i >= 0; i--) {
int digitValue;

if (hex[i] >= '0' && hex[i] <= '9') {
digitValue = hex[i] - '0';
} else if (hex[i] >= 'A' && hex[i] <= 'F') {
digitValue = hex[i] - 'A' + 10;
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
digitValue = hex[i] - 'a' + 10;
}

decimal += digitValue * pow(16, length - 1 - i);
}

return decimal;
}

int main() {
char hexNumber[] = "1A";

int decimalNumber = hexToDecimal(hexNumber);

printf("Method 3 Output: %d\n", decimalNumber);

return 0;
}
``````

### Output:

``````Method 3 Output: 26
``````
• This method utilizes the `pow` function to calculate the positional value of each digit.
• The rest of the logic remains similar to Method 2.
• The `main` function prints the result.

## Conclusion:

In this blog, we have explored three distinct methods for converting hexadecimal numbers to decimal in C. Each method showcased a different approach, from using standard library functions like `strtol` to manual conversion with iterative processes and leveraging the `pow` function for positional value calculation.