Number Bases: Binary, Octal, Decimal and Hex

The same number can be written in base 2, 8, 10 or 16. Here is how place value works, how to convert by hand, and why programmers use hex so much.

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 1011 is 1×8 + 0×4 + 1×2 + 1×1 = 11.
  • Octal 755 is 7×64 + 5×8 + 5×1 = 493.
  • Hexadecimal FF is 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.

DecimalBinaryOctalHex
0000
1111
21022
31133
410044
510155
611066
711177
81000108
91001119
10101012A
11101113B
12110014C
13110115D
14111016E
15111117F
16100002010

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

DivisionQuotientRemainder
202 ÷ 21010
101 ÷ 2501
50 ÷ 2250
25 ÷ 2121
12 ÷ 260
6 ÷ 230
3 ÷ 211
1 ÷ 201

Reading the remainders from bottom to top gives 11001010. Python agrees: bin(202) returns 0b11001010.

202 to hexadecimal

DivisionQuotientRemainder
202 ÷ 161210 (A)
12 ÷ 16012 (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

DivisionQuotientRemainder
202 ÷ 8252
25 ÷ 831
3 ÷ 803

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:

  • 0b11001010 for binary
  • 0o312 for octal
  • 0xCA for hexadecimal
  • 202 with 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 755 is 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, but int("0xCA") raises an error.
  • Forgetting to pad. A byte shown as A is easy to misread; 0A or 00001010 is clearer. Python's format(n, "08b") pads to eight bits.
  • Treating a hex string as text. "41" is two characters, while 0x41 is one byte with the value 65.