# Hexadecimal

"Base 8 is just like Base 10 if you're missing 2 fingers. Shall we have a go at it?" *Tom Lehrer, New Math*

### Symbols

Binary only allows symbols for 0 and 1; anything bigger than 1 is represented with additional 0's and 1's in places with values of 2 raised to some power. 1010 = 1*2^{3} + 0*2^{2} + 1*2^{1} + 0*2^{0} = 8 + 0 + 2 + 0 = 10 (base 10). In base 2, there are 2 symbols (0 and 1). In base 10, there are ten symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). What if we wanted more symbols? We can grab them from the alphabet. The first new symbol would be A, the second B, and so on. This gives us enough symbols to work in Base 36 (if we can tell the difference between the number 0 and the letter O, between the number 1 and the letter l, and so on). In practice, the most popular number system for digital work is hexadecimal or base 16. It has the symbols 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, representing decimal numbers from zero to 15.

### Why Hexadecimal?

The link between binary and hexadecimal is intimate. Here's the list of binary numbers for the 16 unique hexadecimal (or "hex" for short) symbols and their base 10 equivalents:

Binary | Hexadecimal | Base 10 |
---|---|---|

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 | A | 10 |

1011 | B | 11 |

1100 | C | 12 |

1101 | D | 13 |

1110 | E | 14 |

1111 | F | 15 |

Notice how 4 bits can be summarized with a single Hex character. For just 4 bits, this looks like more work than it's worth. However, when we start looking at long strings of numbers, the compactness of Hexadecimal, it's aligned correspondence of 4 bits to 1 symbol, and the related freedom from having to do multiplication and addition to convert back and forth between bases (compare converting from binary to decimal and back) Hex is the most convenient number system with which to work. As we will see, typical conversions between digital and analog representations of quantities use between 8 and 24 bits. At 8 bits, maybe just writing down the bits is adequate. However, 24 bits is a difficult for people to deal with. 24 bits can be expressed as 24/4 = 6 Hex characters. That's much more reasonable.

### Binary to Hex Conversion

Step 1: starting at the RIGHT end of the binary string, group bits into groups of 4. Example:

11010110110110011001111

becomes

110 1011 0110 1100 1100 1111.

What about the 3 bits at the left end? There's an implicit lead 0 (110 = 0110).

Step 2: for each group of 4 bits, substitute the Hex character corresponding to the 4 bit pattern (see table above). Example: from step 1, 0110 = 6, 1011 = B, 0110= 6, 1100 = C (used twice), 1111 = F. In order, that's 6B6CCF.

That's all there is to it!

Exercise \(\PageIndex{1}\)

Express 11110000110101101101101 in Hex.

### Hex to Binary Conversion

This is just the reverse of the previous conversion.

Step 1: for each Hex character, transcribe the 4 bit binary number corresponding to the symbol.

Step 2: if you left spaces between the bits, remove them.

Example: DE32 gives 1101 1110 0011 0010.

Exercise \(\PageIndex{2}\)

What binary number corresponds to 123ABC?

### Hex to Decimal Conversion

Here we need to know the place values for each Hex symbol. The least significant (right-most) place is the 1's place. Next to the left is the 16's (16^{1}). Then comes 16^{2} = 256. Next is 16^{3} = 256*16 = 4096. The next 3 places are 16^{4} = 65536, 16^{5} = 1,048,576, and 16^{6} = 16,777,216. With 64 bit computers becoming common and 128 machines on the horizon, having 16^{16} tabulated is handy, and having 16^{32} available will matter within the professional lifetime of anyone using this module.

To convert a 4 place Hex number to decimal, we simply multiply the value in each place times the power of 16 represented by that place. D3F2 = D*16^{3} + 3*16^{2} + F*16^{1} + 2*16^{0} = 13*4096 + 3*256 + 15*16 + 2 = 54,258.

Exercise \(\PageIndex{3}\)

Convert AB5D to decimal.

### Decimal to Hex Conversion

Step 1: find the highest power of 16 that is LESS than the number being converted. Call this power M so we have a symbol for the power.

Step 2: divide the number by the 16^{M}. The integer part of the quotient will be between 1 and 15. Record this number (in Hex form from the table at near the top of this web page) as the lead symbol. The remainder is the additional part of the number that still must be converted.

Step 3: If the remainder is bigger than 16^{M-1}, go back to Step 2 (using 16^{M-1} instead of 16^{M}) to find the next symbol, which will go one space to the right of the first symbol. If the remainder is smaller than 16^{M-1}, put a 0 in the place to the right of the lead symbol, and check if the remainder is bigger than 16^{M-2}. If it is, go to step 2, using 16^{M-2} instead of 16^{M}, otherwise recording a 0, looking at 16^{M-3}, and so on until digits have been accounted for i.e. a symbol has been placed in the 16^{0} place.

Example \(\PageIndex{1}\):

Convert 12345678 into hexadecimal representation.

**Solution**

This number is between 16^{5} and 16^{6}, so there will be 6 Hex symbols. 12345678/16^{5} = 12345678/1048576 = 11.77... on a calculator, so the symbol here is the Hex representation of 11 = B.

\[12345678 - B*1048576 = 12345678 - 11*1048576 = 12345678 - 11534336 = 811342.\]

The next smaller power of 16, 16^{4} = 65536. This is smaller than 811342, so the symbol in the 16^{4} place is >0. 811342/65536 = 12.38..., the symbol for 12 is C, and the remainder in this position is

\[811342 - C*65536 = 811342 - 12*65536 = 24910\]

At this point, the conversion result is BCxxxx.

Next, 16^{3} = 4096, 24910/4096 = 6.08, symbol is 6, and that's the same Hex or decimal. BC6xxx, and the remainder is 24910-6*4096 = 334. 16^{2} = 256, 334/256 = 1, remainder 334-256 = 78. BC61xx. 78/16 = 4, remainder 78-64 = 14, whose symbol is E, and the final conversion is BC614E.

Exercise \(\PageIndex{4}\)

Convert 87654321 to hexadecimal representation.

**Exercise**: