codescracker
c

C Program to Convert Hexadecimal to Decimal



« Previous Program Next Program »

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

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

Then refer to Hexadecimal to Decimal conversion step by step process. Now let's move on to the program.

Hexadecimal to Decimal in C

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

// Hexadecimal to Decimal Program in C
// ----codescracker.com----

#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 under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of the sample run:

c hexadecimal to decimal

Now supply any hexadecimal number as input say A67 and press 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 decimal ?

The program given above is only correct when user supply any hexadecimal number as input (without any decimal point). But if he/she enters any hexadecimal number that includes point (decimal point), then you have to modify the above program with the program given below. The question is, Write a program in C that converts hexadecimal (includes decimal point) to decimal. The answer to this question is:

// Hexadecimal (including decimal point) to Decimal in C
// ----codescracker.com----

#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 the sample run of above program. This is the final snapshot:

hexadecimal to decimal in c

Program Explained

using User-defined Function

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

// Hexadecimal to Decimal using Function in C
// ----codescracker.com----

#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

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker