🍋
Menu
How-To Beginner 3 min read 621 words

How to Convert Between Number Systems (Binary, Hex, Octal)

Understanding binary, hexadecimal, and octal number systems is fundamental for developers and anyone working with low-level data. This guide explains the conversion logic step by step with practical examples.

Key Takeaways

  • Computers operate in binary (base-2) because digital circuits have two states: on and off.
  • The familiar system using digits 0 through 9.
  • To convert 42 to binary: 42 / 2 = 21 remainder 0, 21 / 2 = 10 remainder 1, 10 / 2 = 5 remainder 0, 5 / 2 = 2 remainder 1, 2 / 2 = 1 remainder 0, 1 / 2 = 0 remainder 1.
  • CSS hex colors encode RGB values in hexadecimal.
  • Memorize powers of 2 up to 2^10 (1024).

Why Multiple Number Systems Exist

Computers operate in binary (base-2) because digital circuits have two states: on and off. Humans prefer decimal (base-10) because we have ten fingers. Hexadecimal (base-16) and octal (base-8) serve as convenient shorthand for binary values — hex maps cleanly to 4-bit groups and octal to 3-bit groups, making them far more readable than long binary strings.

The Four Common Bases

Binary (Base-2)

Uses digits 0 and 1. Each position represents a power of 2. The binary number 1011 means (1 x 8) + (0 x 4) + (1 x 2) + (1 x 1) = 11 in decimal. Binary is the native language of computers and appears in networking (IP addresses, subnet masks), permissions (Unix file modes), and bit manipulation.

Octal (Base-8)

Uses digits 0 through 7. Each octal digit maps to exactly 3 binary digits: octal 7 = binary 111, octal 5 = binary 101. Octal is commonly used for Unix file permissions (755 = rwxr-xr-x) and some legacy computing contexts. To convert octal to binary, simply expand each octal digit to its 3-bit binary equivalent.

Decimal (Base-10)

The familiar system using digits 0 through 9. Converting from decimal to other bases requires repeated division: divide the number by the target base, record the remainder, then divide the quotient again until it reaches zero. The remainders read bottom-to-top form the converted number.

Hexadecimal (Base-16)

Uses digits 0-9 and letters A-F (where A=10, B=11, C=12, D=13, E=14, F=15). Each hex digit maps to exactly 4 binary digits: hex F = binary 1111, hex A = binary 1010. Hex is ubiquitous in computing: color codes (#FF6B35), memory addresses (0x7FFE), MAC addresses, and byte-level data inspection.

Conversion Methods

Decimal to Binary (Repeated Division)

To convert 42 to binary: 42 / 2 = 21 remainder 0, 21 / 2 = 10 remainder 1, 10 / 2 = 5 remainder 0, 5 / 2 = 2 remainder 1, 2 / 2 = 1 remainder 0, 1 / 2 = 0 remainder 1. Reading remainders bottom-to-top: 101010. So decimal 42 = binary 101010.

Binary to Hex (Grouping)

Group binary digits into sets of 4 from right to left, padding with leading zeros if needed. Binary 101010 becomes 0010 1010, which maps to 2 A, giving hex 2A. This grouping method is instant once you memorize the 16 possible 4-bit values.

Hex to Decimal (Positional Expansion)

Multiply each hex digit by its positional power of 16. Hex 2A = (2 x 16) + (10 x 1) = 32 + 10 = 42 decimal. For larger hex values, work right to left: hex FF = (15 x 16) + (15 x 1) = 255.

Practical Applications

Color Codes

CSS hex colors encode RGB values in hexadecimal. The color #FF6B35 breaks down as: Red = FF (255), Green = 6B (107), Blue = 35 (53). Understanding this mapping lets you mentally estimate a color from its hex code.

Bitwise Operations

Developers use binary thinking for flag fields and bitmasks. If a user has permissions stored as binary 110 (decimal 6), that means bits 1 and 2 are set while bit 0 is not. Hex makes this more readable for larger flag fields: hex 0x0F masks the lower 4 bits.

Network Addresses

IPv4 addresses are four decimal octets, each representing 8 bits. The subnet mask 255.255.255.0 in binary is 11111111.11111111.11111111.00000000 — the boundary between network and host bits becomes visually obvious in binary.

Tips for Mental Conversion

Memorize powers of 2 up to 2^10 (1024). Know the hex-binary table by heart: 0=0000, 1=0001, through F=1111. Practice converting small numbers (under 256) regularly — speed comes with repetition, not with formulas.

相关工具

相关指南