Convert Hexadecimal to Decimal in C


Prev Tutorial Next Tutorial

Convert Hexadecimal to Decimal Program in C

In this types of program we takes a Hexadecimal number as input and converts it into Decimal number. To convert Hexadecimal to Decimal number first accept Hexadecimal as a input then convert into equivalent Decimal number

Convert Hexadecimal to Decimal in C

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

void main()
{
    char hex[32]={0};
    int  dec,i;
    int  cnt;   /*for power index*/
    int  dig;   /*to store digit*/
    clrscr();
    printf("Please Enter any Hexadecimal Number: ");
    gets(hex);
 
    cnt=0;
    dec=0;
    for(i=(strlen(hex)-1);i>=0;i--)
    {
        switch(hex[i])
        {
            case 'A':
                dig=10; break;
            case 'B':
                dig=11; break;
            case 'C':
                dig=12; break;
            case 'D':
                dig=13; break;
            case 'E':
                dig=14; break;
            case 'F':
                dig=15; break;
            default:
                dig=hex[i]-0x30;
       }
       dec= dec+ (dig)*pow((double)16,(double)cnt);
       cnt++;
   }
 
   printf("Equivalent Decimal Value is: %d",dec);
   getch(); 
}

Output 1

Please Enter any Hexadecimal Number: E1
Equivalent Decimal Value is: 225

Output 1

Please Enter any Hexadecimal Number: AD3
Equivalent Decimal Value is: 2771

Prev Tutorial Next Tutorial

Download Projects

Google Adsense Advertisements
Free Classified Site – Bedpage

Yahoo Advertisements