Number Base Converter — TypeScript source
Convert numbers between binary, octal, decimal and hexadecimal. BigInt-powered, so it handles arbitrarily large values without precision loss.
This is the TypeScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Arbitrary-precision base conversion (BigInt). Bases 2-36. Pure + deterministic
// - the unit-test surface for the Number Base Converter tool.
// Digit math lives in bigintBaseCore; this module defines the null-on-failure
// contract (prefixes stripped only when they match the base).
import { parseDigits, formatDigits } from './bigintBaseCore';
export function parseBigInt(str: string, base: number): bigint | null {
const r = parseDigits(str, base, { strictPrefix: true });
return r.ok ? r.value : null;
}
export function formatBigInt(value: bigint, base: number): string {
return formatDigits(value, base, {});
}
export function convertBase(value: string, fromBase: number, toBase: number): string | null {
const n = parseBigInt(value, fromBase);
if (n === null) return null;
return formatBigInt(n, toBase);
}
export interface BaseRow {
base: number;
text: string;
}
const ALL_BASES_DEFAULT = [2, 8, 10, 16, 32, 36];
/** The bases the all-bases panel shows — exported for callers that already
* hold a parsed BigInt and want the rows without re-parsing (single-parse
* pattern in the island). */
export const DEFAULT_BASES = ALL_BASES_DEFAULT;
/** The same value rendered in several bases at once (live all-bases view).
* Returns null when the input does not parse in fromBase. */
export function allBases(value: string, fromBase: number, bases: number[] = ALL_BASES_DEFAULT): BaseRow[] | null {
const n = parseBigInt(value, fromBase);
if (n === null) return null;
return bases.map((base) => ({ base, text: formatBigInt(n, base) }));
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →