Skip to content
Guides

How to convert between binary, octal, decimal, and hex

The four bases every programmer should be able to translate by feel — and when to reach for a tool instead of doing it in your head.

May 26, 20264 min read
CONVERTNumber BasesGUIDEDEC42BIN101010HEX2AOCT52One number, four bases

Modern programmers spend most of their day in decimal. But the moment you touch color codes, file permissions, memory addresses, or bitmasks, you need fluency in at least one other base. The Number Base Converter is the cheat sheet you can run in your browser.

The four bases, in one paragraph

Decimal (base 10) is what humans count in. Binary (base 2) is what computers count in — every digit is 0 or 1. Hex (base 16) is decimal's bigger sibling; it groups four binary digits into one hex digit. Octal (base 8) groups three binary digits into one. Hex won; octal mostly survives as a notation for Unix file permissions (chmod 755).

A few mental landmarks: 0xFF is 255 is 11111111 is 0o377. 0xFFFF is 65,535 — the largest 16-bit unsigned integer. 0xFFFFFFFF is just over 4 billion — the largest 32-bit unsigned integer. If you can recognise those three, you can read most addresses and bitmasks by feel.

Conversion patterns worth memorising

Hex ↔ binary is straightforward because each hex digit is exactly 4 bits. Memorise the table once and you never need a converter again:

| Hex | Binary | Decimal | |-----|--------|---------| | 0 | 0000 | 0 | | 1 | 0001 | 1 | | 4 | 0100 | 4 | | 8 | 1000 | 8 | | A | 1010 | 10 | | F | 1111 | 15 |

So 0xA5 is 1010 0101. So 1101 0010 is 0xD2. Practice five times, internalise for life.

Decimal ↔ binary is the conversion you'll always reach for a tool to do. Repeated division by 2 (decimal → binary) and multiplication by powers of 2 (binary → decimal) is slow to do in your head past ~6 bits.

Decimal ↔ hex is doable for small numbers (#FF is 255, #80 is 128, #40 is 64) but quickly becomes tool-territory.

When to use the tool

  • Reading hex color codes. #3B82F6 is what RGB values? Convert each pair — 3B is 59, 82 is 130, F6 is 246 — and you have a sense of how warm/cool the color is.
  • Sanity-checking bitmasks. 0x0F is 0000 1111 (lower nibble). 0xF0 is 1111 0000 (upper nibble). When you see flags & 0x0F in code, you know it's masking off everything but the low four bits.
  • Decoding Unix permissions. chmod 755 is octal 755, binary 111 101 101, meaning owner has read/write/execute, group and other have read/execute.
  • Reading memory addresses. Most addresses are hex. Subtracting two addresses and converting the difference to decimal tells you how many bytes apart they are.

A small note on signed numbers

The converter handles non-negative integers only. Negative numbers have multiple conventional encodings (two's complement at what bit width?), and the right answer depends on what you're representing. For typical day-to-day "what color is this hex code" and "what bits are set in this mask" work, unsigned is enough.

For two's complement at 8/16/32/64 bits, the manual recipe is: invert the bits, add one, treat the result as positive. Most languages have a built-in ~ (NOT) operator to do this. But for one-offs, the cleanest approach is to compute it longhand with the converter open in a tab.

Tags#base#binary#hex#how-to

More from Guides

See all →