Base converter
MDV ToolsType a number in one base. Read it in binary, octal, decimal, hex, base 36 and any other base from 2 to 36, exactly — integers of any size, no floating point, no rounding.
- Read as
- —
- Binary
- —Copied
- Octal
- —Copied
- Decimal
- —Copied
- Hex
- —Copied
- Base 36
- —Copied
- —Copied
- Bit length
- —
- Bytes, big-endian
- —Copied
- Two's complement, 64-bit
- —Copied
- Two's complement, 32-bit
- —Copied
How this works
The input is read one character — one Unicode code point — at a time. A digit is worth 0–9 for
0–9 and 10–35 for a–z in either case; the
running value is multiplied by the base and the digit added — Horner's rule — in
BigInt, so there is no size at which digits start to be lost. BigInt
has no parser for an arbitrary base, which is why this loop exists. A 0b,
0o or 0x prefix stands for base 2, 8 or 16. When it matches the
selected base it is dropped. When the selected base has no digit b, o
or x, the prefix decides the base instead — 0xff with Decimal selected
is read as hex — and the Read as row says so. When the letter is a digit of the selected base,
it stays a digit: in Hex, 0b1 is 177; in base 36, x is worth 33.
Underscores and spaces are separators and ignored; a leading minus makes the value negative,
except on zero. A character that is not a digit of the base stops the parse and is named, with
its code point when it is not plain ASCII, and its position counted in characters. The digit
count beside the input counts digits only — not the sign, the prefix or the separators — and shows
a dash while the input cannot be read. Input
above 10,000 digits is refused rather than attempted.
Every output row is BigInt.prototype.toString(radix), the exact inverse. Other base
takes any whole number from 2 to 36 and is independent of the input base. There is no
Number, no parseInt and no floating point anywhere in the arithmetic,
so 264, 2128 and a 10,000-digit decimal come out to the last digit. "Group
digits" inserts a space every 4 binary digits, 3 octal or decimal digits, 2 hex digits — a
display grouping only, and none for base 36 or Other base. Copy copies the value as shown,
grouping and case included, without the note after it; the parser accepts those spaces back.
Uppercase is likewise display only.
Bit length is the length of the magnitude in binary; zero has none. Bytes are the unsigned
big-endian bytes of a non-negative value, padded to a whole byte. Two's complement at width
w shows a value from −2w−1 up to 2w−1 as its bit pattern
(v mod 2^w) in w/4 hex digits — what a w-bit register holds; when
the value is 2w−1 or more, a signed reader sees a negative number from the same bits,
and the row says which. Outside that range the row says how many bits the value would need.
Nothing is uploaded: there is no server in this file.
The Dark button remembers its setting in this browser's local storage. Nothing you type or drop into the page is stored.