Javatpoint Logo
Javatpoint Logo

Hexadecimal to Decimal in C

What is Hexadecimal?

The hexadecimal is also known as base-16, it is a number system that uses the 16 symbols to represent a particular value, and these symbols are from (0-9 and A-F).

What is Decimal?

The decimal is the number system that represents both the integer and non-integer numbers. This number system is also known as the base-10 system, and it uses 10 symbols from (0-9) to represent a particular value.

Conversion of Hexadecimal to Binary number

We know that hexadecimal number contains 16 symbols, i.e., 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F where A,B,C,D,E,F represents (10,11,12,13,14,15) values. We can find the decimal number by extracting the digits from a hexadecimal number, and starting from the right-most digit of the number. Each digit of the hexadecimal number is multiplied with the base 16. We will store the result of this multiplication in a particular variable.

Let?s understand through an example.

If the hexadecimal number is 5A, then its decimal value would be:

decimal_value = 5*161 + 10*160
                         = 90

Now, we understand the conversion of hexadecimal number into a decimal number diagrammatically:

Hexadecimal to Decimal in C

In the above diagram, we consider 2AB as a hexadecimal number and calculated its decimal value.

2AB (Hexadecimal value) = 2*162 + 10*161, + 11*160
                                           = 683 (Decimal value)

Let?s understand through the program.

In the above program, we find the decimal number of a given hexadecimal number. In the above case, we take input from the user and store its value in the hex variable. After retrieving the input from the user, we calculate the length of the input and store it in the len variable. We have iterated each digit of a hexadecimal number and stores the final result in the decimal_value variable


Hexadecimal to Decimal in C

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA