Hexadecimal to decimal in C
In this post, you will learn how to convert hexadecimal to decimal using C programming language.
Hexadecimal describes a base-16 number system. Hexadecimal Number System is generally used in computer programming and Microprocessors. It is also helpful to describe colours on web pages. The main disadvantage of the Hexadecimal number system is that it may not be easy to read and write for people, and it is also difficult to perform operations like multiplications, divisions using the hexadecimal number system.
A decimal number is a base-10 number system where the range of numbers is 0,1,2,3,4,5,6,7,8,9. The main advantages of Decimal Number System are easy-to-read, used by humans and easy to manipulate.
Convert hexadecimal to decimal using for loop in C
In the given C program, we have used for loop to convert hexadecimal to decimal number.
#include<stdio.h>
#include<string.h>
#include<math.h>
int hedxToDec(char *hex) {
int p = 0;
int decimal = 0;
int val, i;
for(i = strlen(hex) - 1 ; i >= 0 ; --i){
if(hex[i]>='0'&&hex[i]<='9'){
val = hex[i] - '0';
}
else{
val = hex[i] - 'A' + 10;
}
decimal = decimal + val * pow(16 , p);
++p;
}
return decimal;
}
int main() {
char hex[100];
printf("Please enter the hexadecimal: ");
scanf("%s", hex);
printf("\nDecimal to hexadecimal: %d", hedxToDec(hex));
return 0;
}
Output of the above code:
Please enter the hexadecimal: 55
Decimal to hexadecimal: 85
Please enter the hexadecimal: 95
Decimal to hexadecimal: 149
Convert hexadecimal to decimal using while loop in C
In the given C program, we have used while loop to convert hexadecimal to decimal number.
#include<stdio.h>
#include<math.h>
int main()
{
int decimal=0, rem, i=0, len=0;
char hexnum[20];
printf("Please enter the 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("\nPlease enter valid Hexadecimal digit.");
return 0;
}
decimal = decimal + (rem*pow(16, i));
len--;
i++;
}
printf("\nEquivalent Decimal Value: %d", decimal);
return 0;
}
Output of the above code:
Please enter the hexadecimal number: 48
Equivalent Decimal Value: 72
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative