How to Build an Integer Base Converter

Jun 13, 2026 • TypeScript • Injoris Team
Table of contents

What this tool does

An integer base converter takes a whole number written in one base and converts it into equivalent representations in other bases. It is useful when you need to move between:

  • Binary (base 2) for bit-level debugging
  • Octal (base 8) for legacy representations
  • Decimal (base 10) for human-friendly values
  • Hexadecimal (base 16) for IDs, hashes, and low-level data
  • Base 64 (base 64) for compact, URL-friendly numeric strings

Try it here: Integer base converter

How to use the tool

  • Enter the number you want to convert in Input number
  • Set Input base (from 2 to 64)
  • Read or copy the converted values for bases 2, 8, 10, 16, 64, or a Custom base

Notes:

  • The tool converts integers only (no fractions).
  • A leading - is supported for negative values.
  • Digits must match the chosen input base. For example, base 16 allows 0-9a-f (lowercase only).

How the tool is implemented

Pick a digit set (bases 2–64)

The key to multi-base conversion is defining a stable digit alphabet. For bases up to 64, a common choice is:

0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/

This is similar to Base64 characters, but used here as numeric digits (not for encoding bytes).

Parse the input into a BigInt

JavaScript numbers are floating-point and lose precision above 2^53 - 1. A base converter should remain correct for large values, so the implementation parses into a BigInt.

Parsing strategy:

  • Validate each digit is allowed in the fromBase alphabet
  • Accumulate using repeated multiplication and addition:
    • dec = dec * fromBase + digitValue

Re-encode the BigInt into the output base

To format a number in a target base:

  • Repeatedly take the remainder (n % toBase) to get the next digit
  • Divide by the base (n /= toBase) to continue
  • Build the output string from right to left

Core conversion function

const DIGITS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/';
 
export function convertIntegerBase(params: { value: string; fromBase: number; toBase: number }) {
  const { value, fromBase, toBase } = params;
 
  if (fromBase < 2 || fromBase > 64) throw new Error('fromBase must be in [2, 64].');
  if (toBase < 2 || toBase > 64) throw new Error('toBase must be in [2, 64].');
 
  const raw = value.trim();
  if (!raw) throw new Error('Input number is required.');
 
  const sign = raw.startsWith('-') ? '-' : '';
  const digits = sign ? raw.slice(1) : raw;
  if (!digits) throw new Error('Input number is required.');
 
  const fromAlphabet = DIGITS.slice(0, fromBase);
  const toAlphabet = DIGITS.slice(0, toBase);
 
  let n = 0n;
  for (const ch of digits) {
    const idx = fromAlphabet.indexOf(ch);
    if (idx === -1) throw new Error(`Invalid digit "${ch}" for base ${fromBase}.`);
    n = n * BigInt(fromBase) + BigInt(idx);
  }
 
  if (n === 0n) return '0';
 
  let out = '';
  while (n > 0n) {
    out = toAlphabet[Number(n % BigInt(toBase))] + out;
    n /= BigInt(toBase);
  }
 
  return sign ? `-${out}` : out;
}

Design choices:

  • BigInt keeps conversion exact for large integers.
  • Strict digit validation gives clear errors instead of silent corruption.
  • The digit set is case-sensitive by design (base 36 uses lowercase letters first).

Privacy and security

The conversion runs entirely in your browser. Your input is not sent to any server API during conversion. The only network activity is normal page loading (JS/CSS assets) and any general site analytics if enabled by the site.

tools
typescript
bigint
base-conversion