How to convert from decimal number to hexadecimal number.

How to convert base 10 to base 16.

## How to convert from decimal to hex

For decimal number x:

- Get the highest power of 16 that is less than the decimal number x:
max(16

) <^{n}*x*, (*n*= 1,2,3,...) - The high hex digit is equal to the integer if the decimal number x divided by the highest power of 16 that is smaller than x:
*d*= int(_{n}*x*/ 16)^{n} - Calculate the difference Δ of the number x and the hex digit d
^{n}times the power of 16, 16^{n}:Δ =

*x*-*d*× 16_{n}^{n} - Repeat step #1 with the difference result until the result is 0:
*x*= Δ

#### Example

Convert x=603 to hex:

*n*=2, 16^{2}=256 < 603

*n*=3, 16^{3}=4096 > 603

So

*n *= 2

*d*_{2} = int(603 / 16^{2}) = 2

Δ = 603 - 2×16^{2} = 91

*n *= 1, *x* = Δ = 91

*d*_{1} = int(91 / 16^{1}) = 5

Δ = 91 - 5×16^{1} = 11

*n *= 0, *x* = Δ = 11

*d*_{0} = int(11 / 16^{0}) = 11_{10} = B_{16}

Δ = 11 - 11×16^{0} = 0

(*d*_{2}*d*_{1}*d*_{0}) = 25B

So

*x* = 603_{10} = 25B_{16}

How to convert hex to decimal ►