DEV Community

Rasika Dangamuwa
Rasika Dangamuwa

Posted on

Binary, Octal, Decimal, Hex: How Number Base Conversion Actually Works

Every developer runs into number base conversion sooner or later — reading a hex color code, decoding a Unix permission bitmask, or debugging why a flag isn't set the way you expect. Doing it by hand is easy to get wrong, and most online converters are one-directional or bury the answer under ads. Here's a quick refresher on how base conversion actually works, plus a free tool that does all four bases at once.

The four bases you'll actually encounter

  • Binary (base 2) — digits 0–1. What computers use internally at the hardware level.
  • Octal (base 8) — digits 0–7. Mostly seen today in Unix file permissions (chmod 755).
  • Decimal (base 10) — digits 0–9. What humans use day to day.
  • Hexadecimal (base 16) — digits 0–9 plus A–F. A compact shorthand for binary, since each hex digit maps to exactly 4 bits (a nibble).

How the conversion actually works

Every positional number system follows the same rule: a digit's value is the digit itself multiplied by the base raised to the power of its position, counting from zero on the right.

Binary to decimal, for 1101:

(1×2³) + (1×2²) + (0×2¹) + (1×2⁰) = 8 + 4 + 0 + 1 = 13
Enter fullscreen mode Exit fullscreen mode

Hex to decimal, for 1A:

(1×16¹) + (10×16⁰) = 16 + 10 = 26
Enter fullscreen mode Exit fullscreen mode

Decimal to binary works in reverse — divide by 2 repeatedly and read the remainders bottom to top:

13 ÷ 2 = 6 remainder 1
 6 ÷ 2 = 3 remainder 0
 3 ÷ 2 = 1 remainder 1
 1 ÷ 2 = 0 remainder 1
Enter fullscreen mode Exit fullscreen mode

Reading the remainders upward gives 1101 — matching the example above.

Binary to hex is the fastest conversion once you know the trick: group the binary digits into sets of 4 from the right, then convert each group independently. 10111110 splits into 1011 and 1110, which are B and E — so the hex value is BE. Pad the leftmost group with zeros if it's short.

Where this shows up in real code

  • CSS colors. #FF5733 is hex for R:255, G:87, B:51. Converting to decimal is how you'd write the equivalent rgb() value; converting to binary is useful when bitmasking individual color channels.
  • Unix permissions. chmod 755 is octal for binary 111 101 101 — owner gets read/write/execute, group and others get read/execute. Seeing the binary makes it obvious which bits are actually set.
  • Bitwise flags. When debugging flag values in C, Python, or JavaScript, decimal hides which bits are on. Binary makes it immediately readable.
  • Networking. IPv4 subnetting means converting decimal octets (0–255) to binary to reason about subnet masks and CIDR ranges.

Try it without doing the math

I built a free Base Converter that handles all four bases at once — type a number in any base, and binary, octal, decimal, and hex all update instantly. Results come with their standard code prefixes (0b, 0o, 0x) so you can copy them straight into a script, and invalid input for the selected base is flagged immediately instead of silently truncated. No signup, no ads blocking the result.

Base conversion is one of those things every programmer technically knows how to do by hand but nobody wants to — a good converter just removes the friction.

Top comments (0)