Hexadecimal to Decimal in C

To convert a hexadecimal number to decimal in C, you can use the strtol function from the standard library. Here’s an example:

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

int main() {
    char hex[] = "1A"; // Hexadecimal number

    // Convert hexadecimal to decimal
    long decimal = strtol(hex, NULL, 16);

    // Print the result
    printf("Decimal: %ld\n", decimal);

    return 0;

In this example, the strtol function is used to convert the hexadecimal string hex to a decimal value. The third argument, 16, indicates that the input is in hexadecimal format. The result is stored in the decimal variable, which can hold larger decimal values using the long data type. Finally, the decimal value is printed using printf.

When you run this code, it will output:

Decimal: 26

indicating that the hexadecimal number “1A” is equal to the decimal number 26.

What is Hexadecimal?

Hexadecimal is a numbering system that uses base 16. It is commonly used in computer programming and digital systems to represent binary values in a more compact and human-readable format.

In the hexadecimal system, the digits 0-9 represent their corresponding decimal values, and the letters A-F (or a-f) represent the decimal values 10-15, respectively. Here is the conversion table:

Decimal     Hexadecimal
0           0
1           1
2           2
3           3
4           4
5           5
6           6
7           7
8           8
9           9
10          A
11          B
12          C
13          D
14          E
15          F

To represent larger values, hexadecimal digits are combined from right to left, similar to decimal. For example:

Hexadecimal    Decimal
1A             1 * 16^1 + A * 16^0 = 16 + 10 = 26
FF             F * 16^1 + F * 16^0 = 15 * 16 + 15 = 255

Hexadecimal is often used in programming to represent memory addresses, color values, and binary data. It is also used in network addresses (IPv6) and cryptographic systems.

What is Decimal?

Decimal is a numbering system that uses base 10, which is the most commonly used numbering system in everyday life. It is also known as the base-10 system because it uses ten digits from 0 to 9 to represent all possible values.

In the decimal system, the position of each digit represents a power of 10. The rightmost digit represents 10^0 (which is 1), the next digit to the left represents 10^1 (which is 10), the next digit represents 10^2 (which is 100), and so on.

For example, the decimal number 2356 can be expanded as follows:

(2 * 10^3) + (3 * 10^2) + (5 * 10^1) + (6 * 10^0)
= (2 * 1000) + (3 * 100) + (5 * 10) + (6 * 1)
= 2000 + 300 + 50 + 6
= 2356

In the decimal system, each digit can have ten possible values, ranging from 0 to 9. By using these ten digits and the positional value system, decimal numbers can represent a wide range of values.

Decimal numbers are used extensively in everyday life for counting, measuring, and representing quantities. They are the default representation for most numerical calculations and are commonly used in mathematics, finance, and many other fields.

Conversion of Hexadecimal to Binary number:

To convert a hexadecimal number to a binary number, you can follow these steps:

  1. Create a table that maps each hexadecimal digit to its corresponding 4-bit binary representation:
Hexadecimal    Binary
0              0000
1              0001
2              0010
3              0011
4              0100
5              0101
6              0110
7              0111
8              1000
9              1001
A              1010
B              1011
C              1100
D              1101
E              1110
F              1111
  1. Starting from the rightmost digit of the hexadecimal number, replace each digit with its 4-bit binary equivalent from the table.
  2. Concatenate the binary representations of all the digits to obtain the binary representation of the hexadecimal number.

Here’s an example to illustrate the conversion from hexadecimal to binary:

Hexadecimal number: 1A

Using the table, we can find the binary representation of each digit: 1 -> 0001 A -> 1010

Concatenating the binary representations: 00011010

So, the hexadecimal number 1A is equivalent to the binary number 00011010.

It’s worth noting that each hexadecimal digit corresponds to 4 binary bits, allowing for a more compact representation of binary values when working with hexadecimal numbers.

Program of Hexadecimal to Decimal in C:

Certainly! Here’s a C program that converts a hexadecimal number to decimal:

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

int main() {
    char hex[10]; // Hexadecimal number
    long decimal; // Decimal number

    printf("Enter a hexadecimal number: ");
    scanf("%s", hex);

    // Convert hexadecimal to decimal
    decimal = strtol(hex, NULL, 16);

    // Print the result
    printf("Decimal: %ld\n", decimal);

    return 0;

In this program, we use the strtol function to convert the hexadecimal string to a decimal value. The strtol function takes three arguments: the hexadecimal string (hex), a pointer to a char variable (we pass NULL here because we don’t need to store the remaining string), and the base (16 for hexadecimal).

The converted decimal value is stored in the decimal variable. Finally, we print the decimal value using printf.

When you run this program, it will prompt you to enter a hexadecimal number. After you enter the number and press Enter, it will convert it to decimal and display the result.