C program to convert Hexadecimal to Decimal number system

Write a C program to read hexadecimal number from user and convert it to Decimal number system. How to convert from Hexadecimal number system to Decimal number system in C.

Example:
Input hexadecimal number: (1A)16
Output decimal number: (26)10

Required knowledge:

Basic C programming, For loop

Hexadecimal number system

Hexadecimal number system is a base 16 number system. It uses 16 symbols to represent all number i.e. 0 1 2 3 4 5 6 7 8 9 A B C D E F

Decimal number system

Decimal number system is a base 10 number system. It uses 10 symbols to represent all numbers i.e 0 1 2 3 4 5 6 7 8 9

Algorithm to convert Hexadecimal to Decimal

Steps to convert from Hexadecimal number system to decimal number system:
Step 1: Read Hexadecimal number.
Step 2: Set decimal = 0, digit = length_of_hexadecimal_digit - 1 and i = 0.
Step 3: decimal = decimal + (hex[i] * 16 ^ digit).
Step 4: digit = digit - 1.
Step 5: Increment the value of i i.e. i = i + 1.
Step 6: Repeat Step 3-5 till the end of hex digit.

Hexadecimal to Decimal conversion

Hexadecimal to decimal conversion table
Hexadecimal Decimal
00
11
22
33
44
55
66
77
88
99
A10
B11
C12
D13
E14
F15


Program:

/**
 * C program to convert Hexadecimal to Decimal number system
 */

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

int main()
{
    char hex[17];
    long long decimal, place;
    int i = 0, val, len;

    decimal = 0;
    place = 1;

    /*
     * Reads hexadecimal number from user
     */
    printf("Enter any hexadecimal number: ");
    gets(hex);
    
    /* Finds the length of total number of hex digit */
    len = strlen(hex); 
    len--;
    
    /*
     * Converts the hexadecimal number to decimal number
     * using algorithm decimal = decimal + (hex[i] * 16 ^ digit)
     */
    for(i=0; hex[i]!='\0'; i++)
    {
        /*
         * Finds the decimal each equivalent hexadecimal digit
         */
        switch(hex[i])
        {
            case '0':
                val = 0;
                break;
            case '1':
                val = 1;
                break;
            case '2':
                val = 2;
                break;
            case '3':
                val = 3;
                break;
            case '4':
                val = 4;
                break;
            case '5':
                val = 5;
                break;
            case '6':
                val = 6;
                break;
            case '7':
                val = 7;
                break;
            case '8':
                val = 8;
                break;
            case '9':
                val = 9;
                break;
            case 'a':
            case 'A':
                val = 10;
                break;
            case 'b':
            case 'B':
                val = 11;
                break;
            case 'c':
            case 'C':
                val = 12;
                break;
            case 'd':
            case 'D':
                val = 13;
                break;
            case 'e':
            case 'E':
                val = 14;
                break;
            case 'f':
            case 'F':
                val = 15;
                break;
        }

        decimal += val * pow(16, len);
        len--;
    }

    printf("\nHexadecimal number = %s\n", hex);
    printf("Decimal number = %lld", decimal);

    return 0;
} 
Output
Enter any hexadecimal number: 1a

Hexadecimal number = 1a
Decimal number = 26

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment