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 10ten is written as Ahex and 16ten is written as 10hex. 10hex means 1 in sixteens position and 0 in units position, 1 × 16 + 0 × 1 = 10hex. Remember that even though there are 16 numerals in hexadecimal, one of them is 0, so Fhex = 15ten 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 00102 becomes F102hex. 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 00002 to 11112 or 0 to 15. A single digit hexadecimal number has that exact range. Decimal numbers do not have a similar property. Neither [02 .. 1112] nor [02 .. 11112] 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 A1hex to binary, here the hex digit 1 should be converted as 0001 and not 1, so A1hex = 101000012 and not 101012.