What a base is
A number base, also called a radix, is how many distinct digits a counting system uses before it carries into the next column. Everyday counting is base 10, with digits 0 to 9. Computers work in base 2, and programmers lean on base 8 and base 16 as compact ways to write base 2 values. The quantity never changes; only the way it is written does.
Place value
In any base, each column is worth the base raised to a power, counting from zero on the right. In decimal, 123 means 1×100 + 2×10 + 3×1 = 123. The same rule works in every base:
- Binary
1011is 1×8 + 0×4 + 1×2 + 1×1 = 11. - Octal
755is 7×64 + 5×8 + 5×1 = 493. - Hexadecimal
FFis 15×16 + 15×1 = 255.
Hexadecimal needs sixteen digits, so after 9 it borrows letters: A is 10, B is 11, and so on up to F, which is 15. Case does not matter; ff and FF are the same value.
0 to 16 in four bases
This table was generated in Python with format(n, "b"), format(n, "o") and format(n, "X"). Watch where each base runs out of digits and carries: binary at 2, octal at 8, hex at 16.
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 |
| 2 | 10 | 2 | 2 |
| 3 | 11 | 3 | 3 |
| 4 | 100 | 4 | 4 |
| 5 | 101 | 5 | 5 |
| 6 | 110 | 6 | 6 |
| 7 | 111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | A |
| 11 | 1011 | 13 | B |
| 12 | 1100 | 14 | C |
| 13 | 1101 | 15 | D |
| 14 | 1110 | 16 | E |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
Converting decimal to another base
To convert a decimal number, divide by the target base again and again, writing down each remainder, until the quotient reaches zero. The remainders, read from the last one back to the first, are the digits of the answer.
202 to binary
| Division | Quotient | Remainder |
|---|---|---|
| 202 ÷ 2 | 101 | 0 |
| 101 ÷ 2 | 50 | 1 |
| 50 ÷ 2 | 25 | 0 |
| 25 ÷ 2 | 12 | 1 |
| 12 ÷ 2 | 6 | 0 |
| 6 ÷ 2 | 3 | 0 |
| 3 ÷ 2 | 1 | 1 |
| 1 ÷ 2 | 0 | 1 |
Reading the remainders from bottom to top gives 11001010. Python agrees: bin(202) returns 0b11001010.
202 to hexadecimal
| Division | Quotient | Remainder |
|---|---|---|
| 202 ÷ 16 | 12 | 10 (A) |
| 12 ÷ 16 | 0 | 12 (C) |
Bottom to top, the remainders are 12 and 10, which are the hex digits C and A, so the result is CA. Python prints 0xca.
202 to octal
| Division | Quotient | Remainder |
|---|---|---|
| 202 ÷ 8 | 25 | 2 |
| 25 ÷ 8 | 3 | 1 |
| 3 ÷ 8 | 0 | 3 |
The octal form is 312, and oct(202) returns 0o312. To go the other way, multiply each digit by its place value and add, as in the place value section above.
Bits, nibbles and bytes
A bit is one binary digit. Four bits make a nibble, and eight bits make a byte. A byte can hold 256 different values, 0 to 255. Two bytes give 65,536 values and four bytes give 4,294,967,296.
Why hex lines up with 4 bits
Sixteen is 2 to the power of 4, so one hex digit covers exactly the values one nibble can hold. That makes conversion a lookup instead of arithmetic: split the binary number into groups of four from the right and replace each group with one hex digit. For 202, the binary 11001010 splits into 1100 and 1010, which are C and A. A byte is therefore always exactly two hex digits, which is why hex is the standard way to show raw bytes. Octal works the same way with groups of three bits, because 8 is 2 to the power of 3.
Prefixes in source code
Most languages need a hint to know which base a literal is in. Python, JavaScript, Rust and many others accept these forms:
0b11001010for binary0o312for octal0xCAfor hexadecimal202with no prefix for decimal
All four are the same number. One trap: in C, and in older JavaScript code, a plain leading zero means octal, so 010 is 8, not 10. Avoid leading zeros on decimal literals.
Where you meet each base
- Binary
- Bit flags, network masks, and any time you need to see which individual bits are set.
- Octal
- Unix file permissions. Each octal digit is three bits for read, write and execute, so
755is a compact permission set; see chmod and file permissions. - Hexadecimal
- Memory addresses in debuggers, byte dumps, hash values, Unicode code points like
U+00E9, and web colors, where each pair of hex digits is one byte of red, green or blue; see hex and RGB colors. - Decimal
- Anything a person reads as a quantity: counts, sizes, prices and status codes.
Common mistakes
- Reading digits left to right when converting by division. The first remainder is the lowest digit, so read from the bottom up.
- Passing a string with a prefix to a parser that does not expect it. In Python,
int("0xCA", 16)works, butint("0xCA")raises an error. - Forgetting to pad. A byte shown as
Ais easy to misread;0Aor00001010is clearer. Python'sformat(n, "08b")pads to eight bits. - Treating a hex string as text.
"41"is two characters, while0x41is one byte with the value 65.