正在加载,请稍候…

Number Base Conversion: Binary, Octal, Decimal, Hex Explained

Convert numbers between binary (base 2), octal (base 8), decimal (base 10), and hexadecimal (base 16).

What Are Number Bases?

A number base (or radix) defines how many unique digits are available to represent numbers. Each position in a number represents a power of the base.

The four common bases in computing are: Base 10 (Decimal) using digits 0-9 which is the everyday number system; Base 2 (Binary) using digits 0-1 which is the language of computers; Base 16 (Hexadecimal) using digits 0-9 and A-F which provides a compact binary representation; and Base 8 (Octal) using digits 0-7 which is used in Unix file permissions.

The same value 255 is written differently in each base:

Decimal:     255
Binary:      11111111
Hexadecimal: FF
Octal:       377

Why Different Bases?

Binary (Base 2): The Hardware Reality

Digital circuits are fundamentally binary - a transistor is either on (1) or off (0). All computer data is ultimately binary at the hardware level.

Hexadecimal (Base 16): Compact Binary

Binary is verbose. The number 255 requires 8 binary digits but only 2 hex digits. Since one hex digit equals exactly 4 binary bits, hex is a convenient shorthand for binary. One common pattern: 1111 0100 1011 0101 becomes F4B5 in hex.

Hexadecimal is ubiquitous in memory addresses (0x7FFE42A3), color codes (#FF5733), SHA-256 hashes (2cf24dba...), IPv6 addresses (2001:0db8:...), assembly language, and byte representations in debugging.

Octal (Base 8): Unix Legacy

Unix file permissions use octal notation because each octal digit represents exactly 3 bits (one set of read/write/execute permissions):

chmod 755 -> rwxr-xr-x
7 = 111 = rwx (owner)
5 = 101 = r-x (group)
5 = 101 = r-x (others)

Conversion Process

Decimal to Binary

Repeatedly divide by 2, collect remainders (read bottom-to-top):

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
42 decimal = 101010 binary

Binary to Decimal

Multiply each bit by its positional value (powers of 2):

101010 = 1x32 + 0x16 + 1x8 + 0x4 + 1x2 + 0x1
       = 32 + 8 + 2 = 42

Binary to Hex

Group bits in sets of 4 from right:

101010 -> 0010 1010 -> 2A

Integer Representations in Code

// JavaScript
(255).toString(2)   // "11111111" (binary)
(255).toString(8)   // "377" (octal)
(255).toString(16)  // "ff" (hex)

parseInt("FF", 16)        // 255
parseInt("377", 8)        // 255
parseInt("11111111", 2)   // 255

// Literals
0b11111111   // Binary literal (255)
0o377        // Octal literal (255)
0xFF         // Hex literal (255)
bin(255)   # '0b11111111'
oct(255)   # '0o377'
hex(255)   # '0xff'

int('ff', 16)       # 255
int('11111111', 2)  # 255

Signed Integer Representation

Signed integers use two's complement encoding. The most significant bit (MSB) is the sign bit: 0 means positive, 1 means negative. To negate a number, flip all bits and add 1.

+42 in 8-bit: 00101010
-42 in 8-bit: 11010110 (flip bits of 42, then +1)

This is why an 8-bit signed integer ranges from -128 to 127, not -127 to 127.

Floating Point: A Different World

Integer bases do not apply directly to floating-point numbers. IEEE 754 stores numbers in a binary scientific notation: value = sign * mantissa * 2^exponent. This is why 0.1 + 0.2 does not equal exactly 0.3 in most languages - 0.1 and 0.2 do not have exact binary representations.

-> Try the Integer Base Converter