# Binary, Octal, Decimal, and Hexadecimal Explained (With Conversion Methods)

We write numbers in base 10 because we have ten fingers. Computers work in base 2, and programmers often use base 8 and base 16 as compact ways to write binary. Once you see the pattern, conversions become straightforward.

## What a base means

In any base, each digit position is worth a power of the base. In decimal, 345 means 3 x 100 + 4 x 10 + 5 x 1. In binary, each position is a power of 2, so 1101 means 1 x 8 + 1 x 4 + 0 x 2 + 1 x 1, which is 13.

## The four common bases

- Binary (base 2): digits 0 and 1. How hardware stores everything.
- Octal (base 8): digits 0 to 7. Used for Unix file permissions (chmod 755).
- Decimal (base 10): digits 0 to 9. Everyday numbers.
- Hexadecimal (base 16): digits 0 to 9 and A to F. Colors, memory addresses, and byte values.

```
Decimal  Binary     Octal  Hex
10       1010       12     A
255      11111111   377    FF
4095     111111111111  7777  FFF
```

## Converting decimal to another base

1. Divide the number by the target base and record the remainder.
2. Divide the quotient again, repeating until the quotient is 0.
3. Read the remainders from last to first.

Example: 255 in hex. 255 divided by 16 is 15 remainder 15, and 15 divided by 16 is 0 remainder 15. Both remainders are F, so 255 is FF.

## Why hex is so common

One hex digit represents exactly four bits, so one byte is always two hex digits. That is why colors are written like #4338ca, and why 0xFF is 255, the largest value of a single byte. Converting between binary and hex is just grouping bits in fours.

## Prefixes in code

- 0b1010 for binary, 0o12 for octal (or a leading 0 in some older languages), and 0xFF for hex.
- In JavaScript, parseInt('ff', 16) parses hex and (255).toString(2) gives binary.
- In Python, int('ff', 16), bin(255), oct(255), and hex(255) do the conversions.

> **Negative numbers**
>
> Computers usually store negative integers in two's complement form, so the same bits can mean different values depending on whether the number is signed. Check the type when converting negative values.

## Frequently asked questions

### How do I convert binary to decimal?

Add up the powers of 2 for each 1 bit. 1101 is 8 + 4 + 1 = 13.

### Why do programmers use hexadecimal?

Each hex digit maps to four bits, making binary values compact and easy to read.

### What is 255 in binary and hex?

255 is 11111111 in binary and FF in hexadecimal.

### What does chmod 755 mean?

It is an octal permission value: 7 (read, write, execute) for the owner, and 5 (read, execute) for group and others.

**Try it:** [Number Base Converter](https://convert.ilovekit.app/base) — free, runs in your browser, nothing is uploaded.

*Originally published at [ilovekit.app](https://ilovekit.app/blog/binary-octal-decimal-hexadecimal-explained).*
