Hexadecimal numbers

image of base 10

Base 10 (decimal)

As you may know, a decimal number is a number in base 10, and uses ten digits, with the places representing powers of 10. That is, 521 is equal to 5 times 100 (10 to the power 2) plus 2 times 10 (10 to the power 1) plus 1 times 1 (10 to the power 0).

A hexadecimal number is a number in base 16, and uses sixteen symbols, with the places representing powers of 16. The sixteen symbols consist of the digits 0 through 9 and the letters A through F. For example, the number 30 in base ten can be expressed as 1E in hexadecimal, since it is equal to 1 times 16 (16 to the power 1) plus E (that is, 14) times 1 (16 to the power 0).

You may have noticed that 255 in base 10 is one less than 16 squared, just like 99 is one less than 10 squared. That means that any number in the range from 0 to 255 can be expressed in hexadecimal using two symbols, with the first symbol being a zero if the number is 16 or smaller in base 10.


image of base 16

Base 16 (hexadecimal)

To translate a number in the range from 0 to 255 into hexadecimal, find the quotient and remainder when dividing by 16 (the number of times 16 can be subtracted from the number and the number left over, respectively). The hexadecimal representation is the hexadecimal symbol for the quotient followed by the hexadecimal symbol for the remainder.

For example, for the number 255, the quotient is 15 (since 16 times 15 is 240), and the remainder is 15 (since 255 minus 240 is 15). The hexadecimal symbol for 15 is F, so the number 255 can be expressed as FF. As another example, for the number 40, the quotient and remainder are 2 and 8, so its hexadecimal representation is 28.

To represent a colour using hexadecimal, the three pairs of symbols for the red, green, and blue values are joined together to form a six-symbol code preceded by #. Since each of red, green, and blue have the value 255 in base 10 for the colour white, the hexadecimal representation of white is formed by joining three copies of FF to obtain #FFFFFF. Using either upper-case or lower-case letters is fine, so #ffffff could be used instead.