Updated April 1, 2023
Introduction to Hexadecimal in C
In C programming language, a hexadecimal number is a value having a made up of 16 symbols which have 10 standard numerical systems from 0 to 9 and 6 extra symbols from A to F. In C, the hexadecimal number system is also known as base-16 number system. In C there is no data type to store hexadecimal values like float or long or double instead you can store in the integral type of data types. In C programming language, hexadecimal value is represented as 0x or 0X and to input hexadecimal value using scanf which has format specifiers like %x or %X.
Functions of Hexadecimal in C Programming
In this article, we are discussing hexadecimal value in the C programming language. Hexadecimal is also like an integral value that has no separate data type. We already know that there are 16 symbols for hexadecimal values like 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Here A, B, C, D, C, E, F represents 11, 12, 13, 14, 15. Let us see an example and its syntax:
Syntax:
Scanf ("%x", &var_name);
To convert decimal number to hexadecimal number we have few steps to follow:
- Firstly divide the given decimal number by 16. (Consider it as integer division).
- Note down the remainder of the above division of decimal numbers.
- Then divide this remainder by 16. Continue until you get the result as 0. (Consider division as integer division).
- So the hexadecimal value obtained will be the sequence of digits of the remainder from last to first.
Examples of Hexadecimal in C
Let us try to convert a decimal number 590 to hexadecimal value using the above steps:
- Divide 590 by 16 i.e. 590 / 16 result = 36 and remainder =14 ( E is hexadecimal value of 14).
- Divide the obtained result by 16 in the above step, so 36 / 16 result = 2 and remainder = 4 (4 is decimal value).
- Divide the obtained result by 16 in the above step, so 2 / 16 result = 0 and remainder =2 ( 2 as decimal value).
- So the hexadecimal value of decimal number 590 is the sequence of digits of the remainder from last to first which will be 24E.
Example #1
Now let us see the program in C programming for converting decimal number to hexadecimal number:
Code:
#include<stdio.h>
int main() {
long int decNum,rem,quo;
int i=1,j,temp;
char hexadecNum[100];
printf("Enter any decimal number to convert it to hexadecimal Number: ");
scanf("%ld",&decNum);
quo = decNum;
while(quo!=0)
{
temp = quo % 16;
if( temp < 10)
temp =temp + 48; else
temp = temp + 55;
hexadecNum[i++]= temp;
quo = quo / 16;
}
printf("hexadecimal value of decimal number entered is %d: ",decNum);
for (j = i -1 ;j> 0;j--)
printf("%c",hexadecNum[j]);
return 0;
}
Output:
In the above example, we are printing decimal number 590 to its hexadecimal number as 24E.
We can even convert hexadecimal number to decimal number also that is to extract any digit from a hexadecimal number we have to multiply the number with base 16 and then add it to the decimal value. Let us consider an example below to see how we can extract decimal numbers from a hexadecimal number.
Example:
Hexadecimal number = 1AB
As discussed above we saw A represents 10 and B represents 11. Now we will multiply with proper base with power of 16. So
1 = 1 * (16^2) = 256
A = 10 * (16^1) = 160
B = 11 * (16^0) = 11
So now we have to add all these three results to obtain decimal value.
256 + 160 + 11 = 427
Therefore the decimal value for hexadecimal 1AB is 427.
Example #2
Below is the program for converting given a hexadecimal number to decimal number:
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int decnum=0, rem, i=0, len=0;
char hexdecnum[20];
printf("Enter any Hexadecimal Number to convert it to decimal number: ");
scanf("%s", hexdecnum);
while(hexdecnum[i]!='\0')
{
len++;
i++;
}
len--;
i=0;
while(len>=0)
{
rem = hexdecnum[len];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=90)
rem = rem-55;
else
{
printf("\n Invalid Hexadecimal digit");
getch();
return 0;
}
decnum = decnum + (rem*pow(16, i));
len--;
i++;
}
printf("\nDecimal Value of entered Hexadecimal number = %d", decnum);
getch();
return 0;
}
Output:
Enter any Hexadecimal Number to convert it to decimal number: 1AB
Decimal Value of entered Hexadecimal number = 427
In the above program, we are converting a hexadecimal number 1AB to a decimal number as 427.
Recommended Articles
This is a guide to Hexadecimal in C. Here we discuss the Introduction to Hexadecimal in C and its Working as well as Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –