Convert Hexadecimal to Decimal in C

Advertisements

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

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements