Binary, Octal, Decimal, Hex: A Developer's Number System Reference

5 76
calendar_todayschedule3 min read

Number base conversion is one of those skills that feels obscure until you need it — and then you need it constantly. Reading a hex color, debugging a binary flag, checking a Unix file permission: all require thinking across number systems.

Why Four Bases?

  • Base 10 (decimal) — human-native, what we use daily
  • Base 2 (binary) — what hardware actually uses (transistors have two states)
  • Base 16 (hex) — a compact representation of binary: 1 hex digit = 4 bits exactly
  • Base 8 (octal) — 1 digit = 3 bits; used in Unix file permissions, less common today

Conversion Table

| Decimal | Binary | Octal | Hex |

|---|---|---|---|

| 0 | 0000 | 0 | 0 |

| 8 | 1000 | 10 | 8 |

| 10 | 1010 | 12 | A |

| 15 | 1111 | 17 | F |

| 16 | 10000 | 20 | 10 |

| 255 | 11111111 | 377 | FF |

| 256 | 100000000 | 400 | 100 |

Converting in JavaScript

``javascript

// Decimal to binary/octal/hex

(255).toString(2); // "11111111"

(255).toString(8); // "377"

(255).toString(16); // "ff"

// Other bases to decimal

parseInt("ff", 16); // 255

parseInt("377", 8); // 255

parseInt("11111111", 2); // 255

// Number literals (no conversion needed)

const bin = 0b11111111; // 255

const oct = 0o377; // 255

const hex = 0xff; // 255

// BigInt for large numbers (> 2^53)

BigInt("0xffffffffffffffff").toString(10); // "18446744073709551615"

`

Converting in Python

`python
Decimal → binary/octal/hex
bin(255) # '0b11111111'


oct(255) # '0o377'


hex(255) # '0xff'

Without prefix

format(255, 'b') # '11111111'

format(255, 'o') # '377'

format(255, 'x') # 'ff'

format(255, 'X') # 'FF' (uppercase)

Padded to width

format(255, '08b') # '11111111' (8 digits, zero-padded)

format(255, '02x') # 'ff' (2 digits)

Other bases to decimal

int('ff', 16) # 255

int('377', 8) # 255

int('11111111', 2) # 255

`

Hex in the Real World

You encounter hex constantly as a web developer:

CSS colors:
<code>css <p>#2f855a = R:47 G:133 B:90</p> <p>#fff = R:255 G:255 B:255 (shorthand: each digit doubled)</p> </code>

Memory addresses:
<code> <p>0x7fff5fbff8c0 — typical stack address on macOS</p> </code>

Hashes:
<code> <p>sha256: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e</p> </code>

Binary in the Real World

Bitwise flags — Compact boolean sets:
`javascript
const READ = 0b001; // 1


const WRITE = 0b010; // 2


const EXECUTE = 0b100; // 4

const permissions = READ | EXECUTE; // 0b101 = 5

const canWrite = (permissions & WRITE) !== 0; // false

`

IPv4 subnets:
<code> <p>192.168.1.0/24</p> <p>Mask: 11111111.11111111.11111111.00000000 = 255.255.255.0</p> </code>

Octal in the Real World

Unix file permissions are the primary use:

<code>bash <p>chmod 755 script.sh</p> <h1>7 = 111 (rwx for owner)</h1> <h1>5 = 101 (r-x for group)</h1> <h1>5 = 101 (r-x for others)</h1> </code>

Each digit encodes read (4) + write (2) + execute (1).

Common Gotchas

Old JavaScript octal literals — 010 was octal (= 8) in ES3. Use 0o10 in modern JavaScript to be explicit.

Signed vs unsigned bytes — 0xFF = 255 unsigned, but -1 in a signed 8-bit context (two's complement). Python's int('ff', 16) always returns 255; conversion to signed requires extra logic.

Uppercase vs lowercase hex — 0xFF and 0xff are equivalent. Uppercase is common for addresses; lowercase for CSS colors.

Quick Conversions Without Code

For one-off lookups — a hex color, a binary value, a file permission — SnappyTools' number base converter converts across all four bases simultaneously. Type any value in any base and the others update live.

Summary

  • Binary and hex are the most frequently needed in web/systems work
  • JavaScript: .toString(base) and parseInt(str, base) for all conversions
  • Python: bin(), oct(), hex() and int(str, base) — with format()` for fine control
  • Quick lookups: snappytools.app/number-base-converter/

Originally published at https://snappytools.app/number-base-converter/

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelskiverified - Mar 19

Binary, Octal, Decimal, Hex: The Number Base Cheat Sheet Every Developer Needs

SnappyTools - May 8

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelskiverified - Apr 23

Sovereign Intelligence: The Complete 25,000 Word Blueprint (Download)

Pocket Portfolio - Apr 1
chevron_left
2.2k Points81 Badges
90Posts
0Comments
SnappyTools builds free, fast, browser-based tools for developers, writers, and designers. No signup... Show more

Related Jobs

View all jobs →

Commenters (This Week)

16 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!