# Conversion between hexadecimal and binary numbers

Conversion between hexadecimal and binary is quite easy; which is precisely why hexadecimal became rather important in computer science. This is despite being neither machine friendly like binary nor familiar to humans like decimal.

## About Hexadecimal

Hexadecimal number system has 16 numerals. In addition to the ten decimal numerals [0..9], it also has A, B, C, D, E, and F. For example, in hexadecimal 10_{ten} is written as A_{hex} and 16_{ten} is written as 10_{hex}. 10_{hex} means 1 in sixteens position and 0 in units position, 1 × 16 + 0 × 1 = 10_{hex}. Remember that even though there are 16 numerals in hexadecimal, one of them is 0, so F_{hex} = 15_{ten }is the largest single digit hexadecimal number.

Hexadecimal | Decimal | Binary |
---|---|---|

0 | 0 | 0000 |

1 | 1 | 0001 |

2 | 2 | 0010 |

3 | 3 | 0011 |

4 | 4 | 0100 |

5 | 5 | 0101 |

6 | 6 | 0110 |

7 | 7 | 0111 |

8 | 8 | 1000 |

9 | 9 | 1001 |

A | 10 | 1010 |

B | 11 | 1011 |

C | 12 | 1100 |

D | 13 | 1101 |

E | 14 | 1110 |

F | 15 | 1111 |

## Benefits over binary

Dealing with large binary numbers manually is tedious and it is easy to make mistakes. Binary numbers can be written in a much more concise form using the hexadecimal system. For example, the binary number 1111 0001 0000 0010_{2} becomes F102_{hex}. Binary is the reality when working with computers. But the need for manual interaction with binary numbers can be reduced significantly with the help of hexadecimal.

## Benefits over decimal

Why is the conversion between binary and hexadecimal easier than the conversion between binary and decimal? A 4-digit binary number can represent numbers ranging from 0000_{2} to 1111_{2} or 0 to 15. A single digit hexadecimal number has that exact range. Decimal numbers do not have a similar property. Neither [0_{2} .. 111_{2}] nor [0_{2} .. 1111_{2}] is the same as [0..9]. Hexadecimal has this property only because its base is a power of 2.

What this allows us to do is to convert binary to hexadecimal by using the following simple steps. First, split the binary number into chunks of 4-bits starting from the right. Then convert each 4-bit chunk separately to its equivalent single digit hex number.

Conversion from hexadecimal to binary is almost the opposite. Take each digit from the hexadecimal number and convert it to binary. There is one special rule to follow. Each digit from the hex number should be converted to 4-bit binary chunks. For hex digits whose binary representations are smaller than 4-bits, zero-padding on the left must be done to make the chunk 4 bits.

For example, let us say we want to convert A1_{hex} to binary, here the hex digit 1 should be converted as 0001 and not 1, so A1_{hex} = 10100001_{2} and not 10101_{2}.