# Hex counting

on Friday, December 22, 2006In the latest comic strip of Raulito el friki the hexadecimal numeration is mentioned and some readers are sorry not to dominate it. Here comes the explanation.

The decimal numeration begins with 0 and when the 9 -the last symbol- is reached, the next is 0 and we carry 1 to add it to the following more significant digit; i.e., if we add 1 plus 9, the result is 0 and we carry 1 for the following digit.

In hexadecimal, symbols don't end with 9, after this came A, B, ..., F and only after F is when we come back to 0 and add 1 to the following digit. So, the first 17 numbers in hexadecimal are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10.

If for example, we have the number 39, the following would be 3A. The one that comes after 99, the 9A. After 4F comes 50. And after 9F comes A0.

To be able to add and multiply or to convert between decimal and hexadecimal the most usual is to pass through binary. In binary only to symbols are used: 1 and 0, and the value of every digit is 2^{position}. For example, in the number 11001 the values are:

2^{4} | 2^{3} | 2^{2} | 2^{1} | 2^{0} |

1 | 1 | 0 | 0 | 1 |

So 11001_{BIN} = 1*2^{4} + 1*2³ + 0*2² + 0*2¹ + 1*2^{0} = 8 + 4 + 0 + 1 = 13_{DEC}. We have converted from binary to decimal.

The reverse process, from decimal to binary, is also too simple, you have only to write the number as powers of 2 without repeating any, the exponents are the positions of the ones and the rest are zeros:

28_{DEC} = 2^{4} + 2³ + 2² = 11100_{BIN}.

And now we will finally see how to convert from decimal to hexadecimal through binary. First of all, we have to convert the number from decimal to binary and with the resulting binary we take the bits four by four and represent their hexadecimal value. Let's see it with an example.

303_{DEC} = 2^{5} + 2^{4} + 2³ + 2² + 2¹ + 2^{0} = 100101111 _{BIN} = 0001 0010 1111 _{BIN}

0001_{BIN} = 1_{HEX}

0010_{BIN} = 2_{HEX}

1111_{BIN} = F_{HEX}

303_{DEC} = 12F_{HEX}

To convert from hexadecimal to binary, the reverse process is applied: we write every hexadecimal digit with its binary representation with four digits and then we convert the resulting binary number to decimal.

In the comic strip, the hexadecimal representation of the colors is mentioned. In HTML the colors are represented by a number of six hexadecimal digits, in which the first two correspond to red, the following two to green, and the last two to blue. Being FF_{HEX} = 255_{DEC} the maximum value they can take.

837BA5 =
(Red = 83_{HEX}, Green = 7B_{HEX}, Blue = A5_{HEX}) =
(Red = 131_{DEC}, Green = 123_{HEX}, Blue = 165_{HEX}) =