Number Base Converter — Binary, Octal, Decimal, Hexadecimal
Every number system uses a base (radix) that defines how many unique digits it uses. Binary (base 2) uses 0–1, octal (base 8) uses 0–7, decimal (base 10) uses 0–9, and hexadecimal (base 16) uses 0–9 plus A–F. Programmers constantly switch between these bases when working with memory addresses, CSS colors, file permissions, and bit manipulation.
4 bits = 1 hex digit. 3 bits = 1 octal digit. Two hex digits = 1 byte (0x00–0xFF).
Decimal to Binary Conversion
To convert decimal to binary, repeatedly divide by 2 and record the remainders. Read the remainders from bottom to top. Example: 42 in binary — 42÷2=21 R0, 21÷2=10 R1, 10÷2=5 R0, 5÷2=2 R1, 2÷2=1 R0, 1÷2=0 R1 → reading bottom-up: 101010₂. To verify: 32+0+8+0+2+0 = 42. For the reverse (binary to decimal), multiply each bit by its positional power of 2 and sum: 1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 42.
Hexadecimal and Bytes
Hexadecimal is the dominant notation in low-level programming because each hex digit represents exactly 4 bits, and two hex digits represent exactly 1 byte. This makes hex dumps of memory easy to read — 0xFF = 11111111₂ = 255₁₀ (one full byte). CSS colors use 3 bytes: #RRGGBB where each channel is 0x00–0xFF. Network addresses, cryptographic hashes (MD5, SHA-256), and UUID values are all displayed in hex for compactness.
Octal and Unix Permissions
Octal (base 8) uses 3 bits per digit, which maps perfectly to the Unix permission triplet (read, write, execute). chmod 755 means: owner = 7 (111₂ = rwx), group = 5 (101₂ = r-x), others = 5 (101₂ = r-x). chmod 644: owner = 6 (110₂ = rw-), group = 4 (100₂ = r--), others = 4 (r--). Knowing octal makes reading and setting Unix permissions fast and intuitive.
Quick Reference
- Binary (base 2): digits 0–1. Each digit = 1 bit
- Octal (base 8): digits 0–7. Each digit = 3 bits
- Decimal (base 10): digits 0–9. Standard human notation
- Hexadecimal (base 16): digits 0–9, A–F. Each digit = 4 bits
- 2 hex digits = 1 byte (0x00 to 0xFF)
- Hex prefixed with 0x in C/C++/Java/JS, # in CSS colors
Frequently Asked Questions
How do I convert a decimal number to binary?
Repeatedly divide the decimal number by 2 and record the remainder (0 or 1) each time. Read the remainders from bottom to top to get the binary result. For example: 13 ÷ 2 = 6 R1, 6 ÷ 2 = 3 R0, 3 ÷ 2 = 1 R1, 1 ÷ 2 = 0 R1. Reading remainders bottom-up: 1101₂. To verify: 8+4+0+1 = 13.
Why do computers use binary?
Computers are built from transistors that have only two reliable states: on (1) and off (0). Binary directly maps to these physical states. Using more than two states per storage unit (e.g. base-10) would require far more precise voltage discrimination, making hardware less reliable and more expensive. Binary arithmetic is also simpler to implement in logic gates.
What is hexadecimal used for in web development?
Hexadecimal is used extensively for CSS colors (#FF5733 = red 255, green 87, blue 51), memory addresses (0x7FFF0000), Unicode code points (U+1F600 = emoji), and byte representations in debugging tools. Base 16 is convenient because each hex digit maps exactly to 4 bits (one nibble), making binary-to-hex conversion trivial.
How do I convert a hex color code to RGB?
Split the 6-digit hex code into three pairs. Convert each pair from hexadecimal to decimal. Example: #3A8FCC → R: 3A₁₆ = 58, G: 8F₁₆ = 143, B: CC₁₆ = 204 → rgb(58, 143, 204). The tool handles this automatically — just enter the hex value without the # prefix.
What is an octal number and when is it used?
Octal uses digits 0–7 (base 8). It is used mainly in Unix/Linux file permissions — e.g. chmod 755 means owner=7 (rwx), group=5 (r-x), others=5 (r-x). Each octal digit maps to exactly 3 bits, making it a compact way to express 3-bit permission flags. It is less common in modern programming than hex.
Why is base 16 more popular than base 8 in programming?
Hexadecimal maps perfectly to 8-bit bytes: two hex digits represent exactly one byte (0x00 to 0xFF = 0–255). This alignment makes hex ideal for memory dumps, color codes, and network addresses. Octal (base 8) maps to 3 bits per digit, which doesn't divide evenly into 8-bit bytes and is therefore less convenient for byte-oriented computing.
What is the largest number a 32-bit integer can hold in each base?
A signed 32-bit integer ranges from −2,147,483,648 to 2,147,483,647. The maximum positive value (2³¹−1) is: decimal 2147483647, hex 0x7FFFFFFF, binary 0111 1111 1111 1111 1111 1111 1111 1111 (31 ones). The minimum (−2³¹) in two's complement hex is 0x80000000.