Utility Coder
← Back to Blog
Programming9 min read

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

Master number base conversion between binary, octal, decimal, and hexadecimal. Learn algorithms, use cases, and implementation.

By Andy Pham

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

Understanding number bases is fundamental for programming. This guide covers conversion between binary (base-2), octal (base-8), decimal (base-10), and hexadecimal (base-16).

Number Base Fundamentals

Common Bases

Base Name Digits Prefix Example
2 Binary 0-1 0b 0b1010
8 Octal 0-7 0o 0o755
10 Decimal 0-9 none 255
16 Hex 0-9, A-F 0x 0xFF

Place Values

Decimal 255:
  2   5   5
  |   |   |
  |   |   +-- 5 × 10^0 = 5
  |   +------ 5 × 10^1 = 50
  +---------- 2 × 10^2 = 200
              Total: 255

Binary 11111111:
  1   1   1   1   1   1   1   1
  |   |   |   |   |   |   |   |
  128 64  32  16  8   4   2   1 = 255

Conversion Methods

Any Base to Decimal

function toDecimal(value, base) {
  return parseInt(value, base);
}

// Examples
console.log(toDecimal('1010', 2));    // 10
console.log(toDecimal('777', 8));     // 511
console.log(toDecimal('FF', 16));     // 255
console.log(toDecimal('Z', 36));      // 35

Decimal to Any Base

function fromDecimal(decimal, base) {
  return decimal.toString(base).toUpperCase();
}

// Examples
console.log(fromDecimal(255, 2));     // '11111111'
console.log(fromDecimal(255, 8));     // '377'
console.log(fromDecimal(255, 16));    // 'FF'

Manual Conversion Algorithm

// Decimal to any base (step by step)
function decimalToBase(decimal, base) {
  if (decimal === 0) return '0';

  const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  let result = '';

  while (decimal > 0) {
    const remainder = decimal % base;
    result = digits[remainder] + result;
    decimal = Math.floor(decimal / base);
  }

  return result;
}

// Any base to decimal (step by step)
function baseToDecimal(value, base) {
  const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  value = value.toUpperCase();

  let result = 0;
  for (let i = 0; i < value.length; i++) {
    const digit = digits.indexOf(value[i]);
    result = result * base + digit;
  }

  return result;
}

Common Conversions

Binary ↔ Hexadecimal

Each hex digit = 4 binary digits:

Hex: 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
Bin: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
// Binary to Hex
function binaryToHex(binary) {
  // Pad to multiple of 4
  while (binary.length % 4 !== 0) {
    binary = '0' + binary;
  }

  let hex = '';
  for (let i = 0; i < binary.length; i += 4) {
    const nibble = binary.substr(i, 4);
    hex += parseInt(nibble, 2).toString(16).toUpperCase();
  }

  return hex;
}

// Hex to Binary
function hexToBinary(hex) {
  return hex.split('').map(h =>
    parseInt(h, 16).toString(2).padStart(4, '0')
  ).join('');
}

Binary ↔ Octal

Each octal digit = 3 binary digits:

function binaryToOctal(binary) {
  while (binary.length % 3 !== 0) {
    binary = '0' + binary;
  }

  let octal = '';
  for (let i = 0; i < binary.length; i += 3) {
    const triplet = binary.substr(i, 3);
    octal += parseInt(triplet, 2).toString(8);
  }

  return octal;
}

Use Cases

1. Color Codes (Hex)

// RGB to Hex
function rgbToHex(r, g, b) {
  return '#' + [r, g, b].map(x =>
    x.toString(16).padStart(2, '0')
  ).join('').toUpperCase();
}

// Hex to RGB
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;
}

2. File Permissions (Octal)

// Unix permissions: rwxrwxrwx
function parsePermissions(octal) {
  const binary = parseInt(octal, 8).toString(2).padStart(9, '0');
  const perms = ['r', 'w', 'x'];

  return {
    owner: binary.substr(0, 3).split('').map((b, i) => b === '1' ? perms[i] : '-').join(''),
    group: binary.substr(3, 3).split('').map((b, i) => b === '1' ? perms[i] : '-').join(''),
    other: binary.substr(6, 3).split('').map((b, i) => b === '1' ? perms[i] : '-').join('')
  };
}

console.log(parsePermissions('755'));
// { owner: 'rwx', group: 'r-x', other: 'r-x' }

3. Bitwise Operations

// Flags stored as binary
const READ = 0b001;    // 1
const WRITE = 0b010;   // 2
const EXECUTE = 0b100; // 4

let permissions = READ | WRITE; // 0b011 = 3
console.log((permissions & READ) !== 0);  // true
console.log((permissions & EXECUTE) !== 0); // false

Try Our Tools

Conclusion

Number base conversion is essential for low-level programming, networking, and color manipulation. Understanding the relationship between bases makes debugging and optimization easier.

Share this article