C program to convert Hexadecimal to Decimal

Hexadecimal to Decimal conversion in C:

We can convert any decimal number (base-10 (0 to 9)) into hexadecinal number(base-16 (0 or 9 and A(10),B(11),C(12),D(13),E(14),F(15)) by c program.

Decimal Number

Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 23, 445, 132, 0, 2 etc.

Hexadecimal Number

Hexadecimal number is a base 16 number because it ranges from 0 or 9 and A(10),B(11),C(12),D(13),E(14),F(15). Any combination of digits is Hexadecimal number such as A34, 5A4, 7DE7, ABCD etc.

To understand this example, you should have the knowledge of the following C programming topics:

C Functions
C User-defined functions

/*program to convert number from hexadecimal to decimal*/

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

int main()
{
	char hex[32]={0};
	int  dec,i;
	int  cnt;	/*for power index*/
	int  dig;	/*to store digit*/

	printf("Enter hex value: ");
	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("DECIMAL value is: %d",dec);
	return 0;
}

Output:

Enter hex value: A
DECIMAL value is: 10

Enter hex value: A1
DECIMAL value is: 161

Programming Solutions @ rajeshshukla

All Programs
C Language Simple Programs
C Language If Condition and Switch Statement Programs
C Language Loop Programs
C Language Conversion Programs
C Language Pattern Programs
C Language Single Dimension Numeric Array programs
C Language Double Dimension Numeric Array Programs
C Language Single Dimension Character Array programs
C Language Pointer Programs
C Language File Handling Programs

Tutorials

Technical Questions

Interview Questions

C Programming
C++ Programming
Class 11 (Python)
Class 12 (Python)
C Language
C++ Programming
Python

C Interview Questions
C++ Interview Questions


Number Of Visitors

0238202