Base conversion

O(log_b(a))

Every positive integer can be written uniquely in any base b ≥ 2 by repeatedly dividing by b and collecting remainders. Each remainder is one digit of the result in the new base, produced least-significant-first; reversing gives the final representation. For bases above 9, digits 10–15 are written as letters A–F (hexadecimal convention). This algorithm underlies binary, octal, and hexadecimal representations used throughout computing.

Sequence
Press ▶ to run
Edit the input and press Play

How it works

  1. Clamp the target base to the range 2–16
  2. Divide a by the base; the remainder is the next digit (least significant first)
  3. Prepend the digit to the result; replace a with the quotient
  4. Repeat until a = 0; the collected digits form the base-b representation

Pseudocode

1toBase(a, base):                      # O(log_base(a))2  base = clamp(base, 2, 16)3  digits = []4  while a > 0:5    digits.prepend(DIGITS[a % base])  # least-significant first → prepend6    a = floor(a / base)7  return digits.join("")