The HexaDecimal numeral system is a type of number system in Mathematics and computing that represents a number using a base of 16. This means it has 16 symbols to represent a number.
The symbols are: 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Hexadecimal numbers are widely used in Software development and system designing because they provide a human-friendly representation of binary-coded values.
A number 35 can be a Hexadecimal or a Decimal provided what is its base. If the base is 16 i.e, (35)16, it is a Hex number, and if it is (35)10, then a Dec number
|Decimal Numbers||Hexadecimal Numbers|
Hexadecimal to Decimal Conversion
As we have seen above the Hex system uses base 16, this base is used to convert a Hex number to a Dec number. Each digit of a Hex is multiplied with a power of 16. The power starts at 0 from the right moving forward towards the right with the increase in power. For the conversion to complete, the multiplied numbers are added.
Deimal Number = dn-1 × 16r-1+….+ d2 × 162 + d1 × 161 + d0 × 160.
- n = number of digits.
- r = placement of the digit.
Example: Let’s Convert a hexadecimal number (35)16 to its decimal form.
(35)16 = 3 × 161 + 5 × 160
= 3 × 16 + 5 × 1
= 48 + 5
Therefore, (35)16 = (53)10
Example: Let’s convert another Hexadecimal number (3C)16 to Decimal form.
(3C)16 = (3 × 161) + (12 × 160) = (60)10
Decimal to Hexadecimal Conversion
There are various methods you can convert a decimal number to a hexadecimal number, but the easy one is the division method. First, divide the decimal number by 16, considering the number as an integer, and keep aside the remainder. Again divide the quotient by 16 and repeat till you get the quotient value equal to zero.
Now take the values of the remainder’s left in the reverse order to get the hexadecimal numbers.
Example: Let’s convert the decimal number 30 to hexadecimal
If you see the table above, you will get the point that 1 in Dec corresponds to 1 in Hex and 14 in Dec corresponds to E in Hex.
Converting a Decimal to Hexadecimal in Python Code
Python is a very easy language because of the presence of lots of libraries to ease your task. It has the ‘hex’ class for this purpose. See the code below:
decimal = 30 hexadecimal = hex(dcimal) print(hexadecimal)
We can also do it using Loop
table = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A' , 'B', 'C', 'D', 'E', 'F'] dec = 30 hex = '' while(dec>0): remainder = dec%16 hex = table[remainder]+ hex dec = dec//16 print("Hexadecimal: ", hex)
Converting a Hexadecimal to Decimal in Python Code
Similarly, we can convert Hex to Dec in Python easily, see the code below:
hex = "1E" dec = int(hex, 16) print(dec)