Number Base Conversion Guide

2026-06-04·6 min read·Text

Common Number Bases

A number base (radix) is a way of representing numbers in mathematics, determining the weight of each digit. Binary (Base-2) is the foundation of computers, using only 0 and 1; octal (Base-8) uses 0-7; decimal (Base-10) is the base humans use daily; hexadecimal (Base-16) uses 0-9 and A-F, and is the most commonly used non-decimal base in programming.

Hexadecimal is widely used in programming because it can compactly represent binary data - every 4 bits of binary correspond exactly to 1 hexadecimal digit. For example, binary 1111 1111 can be simply represented as FF. This makes hexadecimal the preferred representation for memory addresses, color values, file hashes, and other scenarios.

Conversion Methods Explained

The core method of base conversion is "division with remainder": continuously divide the decimal number by the target base, record the remainders, then arrange the remainders in reverse order. Reverse conversion multiplies each digit by its corresponding weight and sums the results.

// JavaScript base conversion
const number = 255;

// Decimal to other bases
console.log(number.toString(2));   // '11111111' (binary)
console.log(number.toString(8));   // '377' (octal)
console.log(number.toString(16));  // 'ff' (hexadecimal)

// Other bases to decimal
console.log(parseInt('11111111', 2));  // 255
console.log(parseInt('377', 8));       // 255
console.log(parseInt('ff', 16));       // 255

// Color value conversion example
function hexToRgb(hex) {
  const result = /^#?([a-fd]{2})([a-fd]{2})([a-fd]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}

console.log(hexToRgb('#FF5733'));
// { r: 255, g: 87, b: 51 }

Practical Use Cases

Base conversion has wide applications in programming: CSS color values use hexadecimal representation (e.g., #3498db); MAC addresses use hexadecimal (e.g., 00:1A:2B:3C:4D:5E); memory addresses and debug information commonly use hexadecimal; network protocol analysis requires understanding binary data; file hashes (MD5, SHA) use hexadecimal representation.

Python also provides convenient conversion methods: bin(), oct(), hex() functions convert to binary, octal, and hexadecimal strings respectively. The int() function can convert other base strings to decimal integers.

Recommended Tools

Programmer's Calculator is a calculator supporting multi-base conversion. RapidTables provides online base conversion tools. Bit Calculator is a bitwise operation visualization tool to help understand binary operations. Hex editors can view and edit binary files.