How to convert decimal to hex

 

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:

  1. Get the highest power of 16 that is less than the decimal number x:

    max(16n) < x, (n = 1,2,3,...)

  2. 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:

    dn = int(x / 16n)

  3. Calculate the difference Δ of the number x and the hex digit dn times the power of 16, 16n:

    Δ = x - dn × 16n

  4. Repeat step #1 with the difference result until the result is 0:

    x = Δ

Example

Convert x=603 to hex:

n=2, 162=256 < 603

n=3, 163=4096 > 603

So

n = 2

d2 = int(603 / 162) = 2

Δ = 603 - 2×162 = 91

n = 1, x = Δ = 91

d1 = int(91 / 161) = 5

Δ = 91 - 5×161 = 11

n = 0, x = Δ = 11

d0 = int(11 / 160) = 1110 = B16

Δ = 11 - 11×160 = 0

(d2d1d0) = 25B

So

x = 60310 = 25B16

 

How to convert hex to decimal ►

 

See also