Skip to content

Binary, Decimal and Hexadecimal: A Complete Number Base Conversion Guide

Every number you see on a screen, every color in your CSS, every file permission on a Linux server is ultimately represented using a number system. Most of us think in decimal (base 10) because we grew up counting with ten fingers. But computers think in binary (base 2), and developers frequently work with hexadecimal (base 16) and octal (base 8). Understanding how these number bases work and how to convert between them is a fundamental skill for any programmer.

What Are Number Bases?

A number base, also called a radix, defines how many unique digits a number system uses. In any positional notation system, each digit's value depends on its position. The rightmost digit represents the base raised to the power of 0, the next digit to the left is the base raised to the power of 1, and so on. This is the same principle you already know from decimal. The number 427 means 4 x 100 + 2 x 10 + 7 x 1. The same logic applies to every other base.

Binary (Base 2)

Binary uses only two digits: 0 and 1. This is the language of computers because digital circuits have two states, on and off, which map perfectly to 1 and 0. Every piece of data your computer processes, from text to images to video, is ultimately stored as a sequence of binary digits (bits).

Each position in a binary number represents a power of 2. From right to left, the positions represent 1, 2, 4, 8, 16, 32, 64, 128 and so on. A group of 8 bits forms a byte, which can represent values from 0 to 255.

0b0001 = 1

0b0010 = 2

0b0100 = 4

0b1000 = 8

0b1111 = 15

0b11111111 = 255

Decimal (Base 10)

Decimal is the number system we use every day. It uses ten digits: 0 through 9. Each position represents a power of 10. There is nothing mathematically special about base 10. We use it because humans have ten fingers, which made it the natural choice for counting throughout history. When working with computers, decimal serves as the human-readable bridge between what we understand intuitively and what machines process in binary.

Hexadecimal (Base 16)

Hexadecimal uses sixteen digits: 0 through 9 followed by A through F, where A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15. Hex is extremely popular in programming because it provides a compact way to represent binary data. Every single hex digit maps to exactly four binary digits (bits), which makes conversion between the two trivially easy.

You encounter hexadecimal constantly in development. CSS colors like #FF5733 are hex values. Memory addresses in debuggers are displayed in hex. Unicode code points use hex notation. Error codes, MAC addresses, and cryptographic hashes are all typically shown in hexadecimal.

0x0A = 10

0x1F = 31

0xFF = 255

0x100 = 256

0xFFFF = 65535

Octal (Base 8)

Octal uses eight digits: 0 through 7. While less common in modern programming than hex, octal has one major area where it still dominates: Unix file permissions. The chmod command uses octal values to set read, write and execute permissions. Each octal digit maps to exactly three binary bits, which correspond to the three permission flags.

chmod 755 = rwxr-xr-x (owner: 7=111, group: 5=101, others: 5=101)

chmod 644 = rw-r--r-- (owner: 6=110, group: 4=100, others: 4=100)

chmod 777 = rwxrwxrwx (full permissions for everyone)

How to Convert Between Bases Manually

Binary to Decimal

To convert binary to decimal, multiply each bit by 2 raised to its position (starting from 0 on the right), then add all the results together.

Example: Convert 11010110 to decimal

1x128 + 1x64 + 0x32 + 1x16 + 0x8 + 1x4 + 1x2 + 0x1

= 128 + 64 + 0 + 16 + 0 + 4 + 2 + 0

= 214

Decimal to Binary

To convert decimal to binary, repeatedly divide the number by 2 and record the remainder at each step. Read the remainders from bottom to top.

Example: Convert 156 to binary

156 / 2 = 78 remainder 0

78 / 2 = 39 remainder 0

39 / 2 = 19 remainder 1

19 / 2 = 9 remainder 1

9 / 2 = 4 remainder 1

4 / 2 = 2 remainder 0

2 / 2 = 1 remainder 0

1 / 2 = 0 remainder 1

Read bottom to top:

Result: 10011100

Hex to Decimal and Back

To convert hex to decimal, multiply each digit by 16 raised to its position. To go from decimal to hex, divide repeatedly by 16 and use the remainders.

Example: Convert 0x2F to decimal

2 x 16 + F(15) x 1

= 32 + 15

= 47

Example: Convert 255 to hex

255 / 16 = 15 remainder 15 (F)

15 / 16 = 0 remainder 15 (F)

Result: 0xFF

Binary to Hex (Quick Method)

Because one hex digit equals exactly four bits, you can convert between binary and hex by grouping. Split the binary number into groups of four bits from the right, then convert each group to its hex equivalent.

Example: Convert 11010110 to hex

1101 0110

1101 = D, 0110 = 6

Result: 0xD6

Number Base Conversion in JavaScript

JavaScript provides built-in methods for converting between number bases. The parseInt() function parses a string with a given radix, and toString() converts a number to a string in any base.

// Parsing from different bases to decimal

parseInt("11010110", 2) // 214 (binary to decimal)

parseInt("D6", 16) // 214 (hex to decimal)

parseInt("326", 8) // 214 (octal to decimal)

// Converting decimal to other bases

(214).toString(2) // "11010110" (to binary)

(214).toString(16) // "d6" (to hex)

(214).toString(8) // "326" (to octal)

// Number literals in different bases

0b11010110 // 214 (binary literal)

0xD6 // 214 (hex literal)

0o326 // 214 (octal literal)

Number Base Conversion in Python

Python offers dedicated built-in functions for common base conversions, plus the flexible int() function that accepts any base from 2 to 36.

# Converting decimal to other bases

bin(214) # '0b11010110'

hex(214) # '0xd6'

oct(214) # '0o326'

# Parsing from other bases to decimal

int("11010110", 2) # 214

int("D6", 16) # 214

int("326", 8) # 214

# Format with padding

format(214, "08b") # '11010110' (8-bit padded)

format(214, "02x") # 'd6' (2-digit hex)

Common Use Cases

  • CSS colors. Hex values like #6366f1 are three hex pairs representing red, green and blue channels. Each pair is a value from 0 (00) to 255 (FF).
  • Bitwise operations. Flags, masks, and permissions are often manipulated using binary and displayed in hex. Understanding binary is essential for bitwise AND, OR, XOR and shift operations.
  • Networking. IPv4 addresses are four decimal octets (each 0 to 255), MAC addresses use hex pairs, and subnet masks are clearer when viewed in binary.
  • File permissions. Unix systems use octal notation for chmod. Each digit encodes three permission bits (read, write, execute) for owner, group and others.
  • Memory and debugging. Memory addresses and hex dumps use hexadecimal because it is compact and aligns neatly with byte boundaries.
  • Character encoding. Unicode code points are written in hex (e.g., U+0041 for the letter A). ASCII tables and UTF-8 byte sequences are also commonly shown in hex.
  • Cryptography. Hash digests (MD5, SHA-256) are displayed as hex strings. A SHA-256 hash is 64 hex characters representing 256 bits.

Quick Reference Table

Here is a side-by-side comparison of values in all four common bases:

DecimalBinaryHexOctal
0000000
1000111
5010155
101010A12
151111F17
16100001020
100110010064144
25511111111FF377

Convert between number bases instantly

Enter a number in any base and get instant conversions to binary, decimal, hexadecimal and octal. No server processing, everything runs in your browser.

Open Number Base Converter