# Hexadecimal

The hexadecimal numeral system is a base 16 numeral system. Both the hexadecimal numeral system and the decimal numeral system (the system most of us are accustomed to) are positional numeral systems. They differ in the bases used. As mentioned, the hexadecimal numeral system is a base 16 numeral system, while the decimal numeral system is a base 10 numeral system.

While 10 digits (0-9) are used in the decimal numeral system, 16 are used in the hexadecimal system: 0-9 and A-F. The table below shows the corresponding digits in the decimal (dec) and hexadecimal (hex) numeral systems:

dec | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |

## Decimal numeral system

This section is a review of the decimal numeral system in the context of positional numeral systems. Since both the decimal and hexadecimal numeral systems are positional numeral systems, understanding how these work in the context of the decimal numeral system first can be helpful for working with the hexadecimal numeral system.

Positional numeral systems represent numbers using a given number of digits that correspond to the base used, as well as the position of a given digit within the figure. Together, these are used to generate a number within the system. In the case of the decimal numeral system, there are 10 digits, 0-9, that when written in the form of a numeral (e.g. 5129), can be expanded as follows:

In a positional numeral system, starting from the right of a numeral and moving left, the digit in each position has a value that is the product of the digit and its place value, which is determined by its position. The first digit left of the decimal point is the base raised to the power of 0. In the decimal numeral system, the base is 10, so in the example above, we could write "5129" as "5129." and determine that the first digit left of the decimal point, 9, has a value that is the product of 9 and the place value, or 9 × 10^{0}. This same concept carries through other positional numeral systems, the difference being the base used. In the hexadecimal numeral system, the base is 16, while in the binary numeral system, the base is 2. With this understanding, we can convert between the hexadecimal and decimal numeral system.

## Hexadecimal numeral system

As a positional numeral system, we can expand a hexadecimal number in the same manner as above in order to convert it into a decimal number. Consider the hexadecimal number 10021B0F. The digits 1-9 in the hexadecimal numeral system are equivalent to those in the decimal numeral system, but we need to use the table at the top of the page to determine the digits that correspond to the letters B and F. Then, expanding this numeral as the sum of the products of each digit and their corresponding place value yields:

### Converting from decimal to hexadecimal

To convert from hexadecimal to decimal, start with a series of division by 16 until the quotient is 0, then use the remainders to form the hexadecimal number. For example, the decimal number 456 can be converted to hexadecimal as follows:

Iterations of division by 16 | Remainder | Hexadecimal digit |
---|---|---|

456/16 = 28 | 8 | 8 |

28/16 = 1 | 12 | C |

1/16 = 0 | 1 | 1 |

To form the hexadecimal number, the remainder in each iteration of division by 16 is written to the left of the remainder from the preceding iteration. Thus, 456 in decimal form is 8C1.

## Hexadecimal numbers and digital systems

Hexadecimal numbers provide an efficient way to represent a binary number that may otherwise consist of a long array of digits whose value can be difficult, especially for humans, to interpret. One way to convert a number from binary to hexadecimal is to first convert it to decimal form, then convert its decimal form to hexadecimal in the manner described above. Binary, like decimal and hexadecimal, is a positional numeral system. Specifically, it is a base 2 numeral system in which the 2 digits are 0 and 1. Thus, using the same process described above, we can convert a binary number such as 10111101 as follows:

Then, convert the decimal number 189 to hexadecimal:

Iterations of division by 16 | Remainder | Hexadecimal digit |
---|---|---|

189/16 = 11 | 13 | D |

11/16 | 11 = 0 | B |

Thus, the binary number 10111101 is equivalent to the hexadecimal number BD.

Fortunately, it is possible to simplify the steps of converting a binary number to hexadecimal. By convention, a binary number can be arranged into groups of 4 consecutive binary numbers, called 4-bits. Each 4-bit binary number has a value between 0000 and 1111 and has a numerical decimal equivalence between 0 and 15, so there are only 16 possible 4-bit configurations. Since each decimal number between 0 and 15 corresponds to a single hexadecimal number between 0 and F, each 4-bit binary number can be written consecutively to form the corresponding hexadecimal number. Each 4-bit configuration and their corresponding decimal and hexadecimal values are shown in the table below:

4-bit binary | Decimal | Hexadecimal |
---|---|---|

0000 | 0 | 0 |

0001 | 1 | 1 |

0010 | 2 | 2 |

0011 | 3 | 3 |

0100 | 4 | 4 |

0101 | 5 | 5 |

0110 | 6 | 6 |

0111 | 7 | 7 |

1000 | 8 | 8 |

1001 | 9 | 9 |

1010 | 10 | A |

1011 | 12 | B |

1100 | 13 | C |

1101 | 14 | D |

1110 | 15 | E |

1111 | 16 | F |

For example, the binary number 10111101 can be grouped into two 4-bit groups as 1011 and 1101. 1011 corresponds to 11 in the decimal numeral system, and 1101 corresponds to 13. 11 and 13 in decimal form corresponds to B and D in hexadecimal form, so 10111101 is BD.

Given that a binary number cannot be grouped into 4-bits, to convert it, group it into as many 4-bit numbers as possible starting from the right-most digit. Then, convert the numeral with less than 4 bits to decimal form before finding its hexadecimal equivalence. For example, the binary number 1001011 can be grouped as 101 1011. 101 can be converted as:

Then, 1011 = B, and the hexadecimal number is therefore 5B.