Binary, Octal, Decimal and Hex — How Number Bases Work

numbersbinaryhexadecimal

We usually count in base 10, but computers are more comfortable with base 2. Hexadecimal is a compact shorthand for binary, and octal occasionally shows up in permissions and older systems.

What a base means

A base is how many digits a position can hold. Decimal uses 0 to 9, binary uses 0 and 1, and hexadecimal uses 0 to 9 plus A to F. The value of a digit depends on its position.

The common bases

  • Binary is base 2 and matches the on and off states of digital circuits.
  • Octal is base 8 and appears in some Unix file permissions.
  • Decimal is base 10 and is what people use day to day.
  • Hexadecimal is base 16 and is a compact way to write binary bytes.

Converting in code

JavaScript:

console.log(parseInt('ff', 16)) // 255
console.log((255).toString(2))  // 11111111

Python:

print(int('ff', 16))
print(format(255, 'b'))

PHP:

echo base_convert('ff', 16, 10);
echo decbin(255);

Common pitfalls

  • Losing leading zeros when a fixed-width representation matters.
  • Using values too large for normal integers without BigInt or an arbitrary-precision type.
  • Confusing a hex string with a decimal-looking string.

Frequently asked questions

  • Which bases are supported? Binary, octal, decimal and hexadecimal cover the vast majority of cases.
  • Can I convert very large numbers? Yes, with a converter that uses arbitrary-precision integers.

Need to convert between bases? Try the free Number Base Converter — it supports large values via BigInt.