C Program to Convert Hexadecimal to Decimal

In this article, we will learn how to create a program in C that converts any given hexadecimal number entered by the user at run-time into its equivalent decimal value. We have also created a user-defined function that does the same job.

But before going through the program, if you are not aware of

Then follow the steps in the Hexadecimal to Decimal conversion process. Now let's move on to the program.

Hexadecimal to Decimal in C

To convert a hexadecimal number to a decimal number in C programming, you have to ask the user to enter the hexadecimal number to convert it into a decimal number, and then display its equivalent value in decimal as an output. The question is, "Write a program in C that converts a hexadecimal number to a decimal." The answer to this question is:

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
    int decnum=0, rem, i=0, len=0;
    char hexnum[20];
    printf("Enter any Hexadecimal Number: ");
    scanf("%s", hexnum);
    while(hexnum[i]!='\0')
    {
        len++;
        i++;
    }
    len--;
    i=0;
    while(len>=0)
    {
        rem = hexnum[len];
        if(rem>=48 && rem<=57)
            rem = rem-48;
        else if(rem>=65 && rem<=70)
            rem = rem-55;
        else if(rem>=97 && rem<=102)
            rem = rem-87;
        else
        {
            printf("\nYou've entered an invalid Hexadecimal digit");
            getch();
            return 0;
        }
        decnum = decnum + (rem*pow(16, i));
        len--;
        i++;
    }
    printf("\nEquivalent Decimal Value = %d", decnum);
    getch();
    return 0;
}

As the above program was written in the Code::Blocks IDE, here is the first snapshot of the sample run:

c hexadecimal to decimal

Now supply any hexadecimal number as input, say A67, and press the ENTER key to see its decimal equivalent, as shown in the second snapshot of the same sample run given below:

hexadecimal to decimal conversion c

Program Explained

What if hexadecimal input contains decimals?

The program given above is only correct when the user supplies any hexadecimal number as input (without any decimal point). However, if he/she enters any hexadecimal number that includes a point (decimal point), you must replace the above program with the one below. The question is, "Write a program in C that converts hexadecimal (including the decimal point) to decimal." The answer to this question is:

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
    int decnum=0, decnum1=0, rem, i=0, len=0, lenTemp, dotPos=0;
    float decnum2=0;
    char hexnum[20];
    printf("Enter any Hexadecimal Number: ");
    scanf("%s", hexnum);
    while(hexnum[i]!='\0')
    {
        if(hexnum[i]=='.')
            dotPos = i;
        len++;
        i++;
    }
    len--;
    i=0;
    if(dotPos==0)
    {
        while(len>=0)
        {
            rem = hexnum[len];
            if(rem>=48 && rem<=57)
                rem = rem-48;
            else if(rem>=65 && rem<=70)
                rem = rem-55;
            else if(rem>=97 && rem<=102)
                rem = rem-87;
            else
            {
                printf("\nYou've entered an invalid Hexadecimal digit");
                getch();
                return 0;
            }
            decnum = decnum + (rem*pow(16, i));
            len--;
            i++;
        }
        printf("\nEquivalent Decimal Value = %d", decnum);
    }
    else
    {
        lenTemp = dotPos-1;
        while(lenTemp>=0)
        {
            rem = hexnum[lenTemp];
            if(rem>=48 && rem<=57)
                rem = rem-48;
            else if(rem>=65 && rem<=70)
                rem = rem-55;
            else if(rem>=97 && rem<=102)
                rem = rem-87;
            else
            {
                printf("\nYou've entered an invalid Hexadecimal digit");
                getch();
                return 0;
            }
            decnum1 = decnum1 + (rem*pow(16, i));
            lenTemp--;
            i++;
        }
        lenTemp = dotPos+1;
        i=-1;
        while(lenTemp<=len)
        {
            rem = hexnum[lenTemp];
            if(rem>=48 && rem<=57)
                rem = rem-48;
            else if(rem>=65 && rem<=70)
                rem = rem-55;
            else if(rem>=97 && rem<=102)
                rem = rem-87;
            else
            {
                printf("\nYou've entered an invalid Hexadecimal digit");
                getch();
                return 0;
            }
            decnum2 = decnum2 + (rem*pow(16, i));
            lenTemp++;
            i--;
        }
        decnum2 = decnum1+decnum2;
        printf("\nEquivalent Decimal Value = %0.2f", decnum2);
    }
    getch();
    return 0;
}

Here is a sample run of the above program. This is the final snapshot:

hexadecimal to decimal in c

Program Explained

Hexadecimal to Decimal in C using a User-Defined Function

Now, as shown in the program below, let's create the same purpose program using a user-defined function called HexToDec(). The question is: "Write a program in C that converts hexadecimal to decimal using a user-defined function." The answer to this question is:

#include<stdio.h>
#include<conio.h>
#include<math.h>
int HexToDecY(char hex[]);
float HexToDecN(char hex[]);
static int i, len, dotPos;
int main()
{
    int decnumInt;
    float decnumFlt;
    char hexnum[20];
    printf("Enter any Hexadecimal Number: ");
    scanf("%s", hexnum);
    while(hexnum[i]!='\0')
    {
        if(hexnum[i]=='.')
            dotPos = i;
        len++;
        i++;
    }
    len--;
    i=0;
    if(dotPos==0)
    {
        decnumInt = HexToDecY(hexnum);
        if(decnumInt!=0)
            printf("\nEquivalent Decimal Value = %d", decnumInt);
    }
    else
    {
        decnumFlt = HexToDecN(hexnum);
        if(decnumFlt!=0)
            printf("\nEquivalent Decimal Value = %0.2f", decnumFlt);
    }
    getch();
    return 0;
}
int HexToDecY(char hex[20])
{
    int dec=0, rem;
    while(len>=0)
    {
        rem = hex[len];
        if(rem>=48 && rem<=57)
            rem = rem-48;
        else if(rem>=65 && rem<=70)
            rem = rem-55;
        else if(rem>=97 && rem<=102)
            rem = rem-87;
        else
        {
            printf("\nYou've entered an invalid Hexadecimal digit");
            return 0;
        }
        dec = dec + (rem*pow(16, i));
        len--;
        i++;
    }
    return dec;
}
float HexToDecN(char hex[20])
{
    int dec1=0, rem, lenTemp;
    float dec2=0;
    lenTemp = dotPos-1;
    while(lenTemp>=0)
    {
        rem = hex[lenTemp];
        if(rem>=48 && rem<=57)
            rem = rem-48;
        else if(rem>=65 && rem<=70)
            rem = rem-55;
        else if(rem>=97 && rem<=102)
            rem = rem-87;
        else
        {
            printf("\nYou've entered an invalid Hexadecimal digit");
            return 0;
        }
        dec1 = dec1 + (rem*pow(16, i));
        lenTemp--;
        i++;
    }
    lenTemp = dotPos+1;
    i=-1;
    while(lenTemp<=len)
    {
        rem = hex[lenTemp];
        if(rem>=48 && rem<=57)
            rem = rem-48;
        else if(rem>=65 && rem<=70)
            rem = rem-55;
        else if(rem>=97 && rem<=102)
            rem = rem-87;
        else
        {
            printf("\nYou've entered an invalid Hexadecimal digit");
            return 0;
        }
        dec2 = dec2 + (rem*pow(16, i));
        lenTemp++;
        i--;
    }
    dec2 = dec1+dec2;
    return dec2;
}

Here is the final snapshot of the above program's sample run.

c hexadecimal to decimal using function

Program Explained

Now let's take another sample run; this time we have supplied a hexadecimal number without any decimal point, as shown in the sample run given below. Here is another final snapshot of the sample run:

c program convert hexadecimal to decimal

The same program in different languages

C Quiz


« Previous Program Next Program »