Convert! Base 2, 8, 10, 16
Robin Kim
—September 24, 2014
Let's talk about a few number bases:
- Decimal, or base 10
- Binary, or base 2
- Octal, or base 8
- Hexadecimal, or base 16
I'll cover the basics of these number bases and simple strategies for converting between them.
Expected prior knowledge: how to convert from decimal to binary, and vice-versa. (Even better if you can convert between two arbitrary number bases!)
System | Base | Valid Digits | Example |
---|---|---|---|
Decimal | 10 | 0 1 2 3 4 5 6 7 8 9 | 31410 |
Binary | 2 | 0 1 | 1001110102 |
Octal | 8 | 0 1 2 3 4 5 6 7 | 4782 |
Hexadecimal | 16 | 0 1 2 3 4 5 6 7 8 9 A B C D E F | 13A16 |
The "base" of a number system represents the number of valid digits that may be used in that system. In the decimal system, we have ten digits: 0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
. We are free to pick from any of those without any confusion.
The binary system has 2 digits, and octal system has 8 digits. Things get a little funny with the hexadecimal system, which has 16 digits. Since our "normal" decimal system only has 10 digits, we borrow a few from our alphabet! A (base 16) represents 10 (base 10), B (base 16) represents 11 (base 10), etc.
To quickly convert between these prior listed bases:
- Convert it to binary.
- Convert it to the base you want.
To go from binary to octal, group the digits by 3, and convert each triplet into its octal equivalent. For hexadecimal, group by 4, and convert each quadruple into its hexadeximal equivalent. Changing from either of these bases back to binary is a similar process. For example, F
(base 16) = 15
(base 10) = 1111
(base 2).
Where have you seen hexadecimal before? Perhaps to represent RGB colors on the web!
Here is a slide deck I made to present to my Hack Reactor remote cohort in August 2014. It better illustrates the idea of converting between the bases.