What is Decimal?
The decimal system is a numerical system that denotes both whole and decimal numbers. This numeric system is alternatively referred to as the base-10 system, employing a set of 10 symbols ranging from 0 to 9 to express specific numerical values.
Conversion of Hexadecimal to Binary number
Hexadecimal numbers consist of 16 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. These symbols represent values from 0 to 15. To convert a hexadecimal number to decimal, we extract the digits starting from the rightmost digit. Each digit is then multiplied by 16 raised to a power corresponding to its position, and the products are summed up to obtain the decimal equivalent. This sum is stored in a designated variable.
Let?s understand through an example.
If the hexadecimal representation is 5A, then its equivalent decimal value would be: ```output
include <stdio.h>
include <math.h>
include <string.h>
int main
{
char hex[17]; // declaration of character array.
long long decimal, place;
int i = 0, val, len; // variables declaration
decimal = 0;
/ Input hexadecimal number from user /
printf("Enter any hexadecimal number: ");
gets(hex);
/ Find the length of total number of hex digit /
len = strlen(hex);
len--;
/*
- Iterate over each hex digit
*/
while(hex[i]!='\0')
{
/ To find the decimal representation of hex[i] /
if(hex[i]>='0' && hex[i]<='9')
{
val = hex[i] - 48;
}
else if(hex[i]>='a' && hex[i]<='f')
{
val = hex[i] - 97 + 10;
}
else if(hex[i]>='A' && hex[i]<='F')
{
val = hex[i] - 65 + 10;
}
decimal += val * pow(16, len);
decimal_value = 5*16 1 + 10*16 0 = 90
Now, let's examine the process of converting a hexadecimal number into a decimal number visually:
In the diagram provided, we are treating 2AB as a hexadecimal value and determining its equivalent in decimal.
Calculating the hexadecimal value of 2AB involves multiplying each digit by its corresponding power of 16, resulting in a decimal value of 683.
Let?s understand through the program.
include <stdio.h>
include <math.h>
include <string.h>
int main
{
char hex[17]; // declaration of character array.
long long decimal, place;
int i = 0, val, len; // variables declaration
decimal = 0;
/ Input hexadecimal number from user /
printf("Enter any hexadecimal number: ");
gets(hex);
/ Find the length of total number of hex digit /
len = strlen(hex);
len--;
/*
- Iterate over each hex digit
*/
while(hex[i]!='\0')
{
/ To find the decimal representation of hex[i] /
if(hex[i]>='0' && hex[i]<='9')
{
val = hex[i] - 48;
}
else if(hex[i]>='a' && hex[i]<='f')
{
val = hex[i] - 97 + 10;
}
else if(hex[i]>='A' && hex[i]<='F')
{
val = hex[i] - 65 + 10;
}
decimal += val * pow(16, len);
In this program, we determine the decimal equivalent of a provided hexadecimal number. Initially, user input is collected and assigned to the variable named hex. Following this input acquisition, the program evaluates the length of the input and records this length in the len variable. The program proceeds to iterate through each digit of the hexadecimal number, ultimately storing the resulting decimal value in the decimal_value variable.
Output