Number base converter
Type a whole number of any length, and the converter writes it exactly in binary, octal, decimal, hex, and any other base up to 36. In Auto mode, a 0x, 0o, or 0b prefix sets the input base, and a number without one is read as decimal. It runs in your browser; nothing you enter is uploaded.
| Binary | ||
|---|---|---|
| Octal | ||
| Decimal | ||
| Hexadecimal | ||
| Other base |
How base conversion works
In base b, each digit is multiplied by a power of b, and its position decides which power. Hex 2f is 2 ×
16 + 15 = 47. Going the other way, 47 is 32 + 8 + 4 + 2 + 1, so in binary it's 101111. And since 47 is 5 × 8 + 7, it's
57 in octal. The letters a to f stand for 10 to 15 in hex, and higher bases carry on through the alphabet up to
z, which is 35 in base 36.
One hex digit is exactly 4 bits and one octal digit exactly 3. That's why hex lines up with bytes: ff is
11111111, and every byte is two hex digits.
What you can type
-
The prefixes
0x,0o, and0b, in either case. In Auto mode the prefix picks the base. When you pick a base yourself, you can still type its prefix, but a prefix for a different base is an error. -
Underscores between digits. The converter ignores them, so
1_000_000and0xffff_ffffwork as they do in Python, Rust, and JavaScript source code. - Letters in either case. The results are written in lowercase.
-
A leading minus sign. Negative numbers come out with a minus sign, not in two's complement, so −1 is
-1in hex rather thanff. For the 8-bit two's complement of a negative number, add 256 first: −1 + 256 = 255, which isff.
The converter takes whole numbers only. If you type a digit that doesn't exist in the input base, such as 2 in binary or
g in hex, the error names the character and the digits that base allows.
Why large numbers stay exact
JavaScript numbers are 64-bit floating point, and they hold every integer exactly only up to 253 − 1, or
9,007,199,254,740,991, which JavaScript calls Number.MAX_SAFE_INTEGER. Above that, parseInt('9007199254740993')
returns 9007199254740992, and a converter built on it changes your number without telling you. This converter uses
BigInt, which has
no fixed size, so 2100 converts to 1267650600228229401496703205376 in decimal and to a 1 followed by 25 zeros in hex, every
digit exact.
In code and on the command line
-
Python:
int('ff', 16)gives 255, andint('0xff', 0)reads the base from the prefix.bin(255),oct(255), andhex(255)give'0b11111111','0o377', and'0xff'. Python integers have no fixed size either. -
JavaScript:
BigInt('0xff')parses a prefixed number of any size, andn.toString(2)writes it in another base.parseInt('ff', 16)is fine for numbers below 253. -
Bash:
echo $((16#ff))prints 255 andprintf '%x\n' 255printsff. Bash arithmetic uses 64-bit signed integers, so for larger numbers usebc:echo 'obase=2; 255' | bc.
Questions
Why is 010 read as ten and not eight?
Because Auto mode follows the newer rule, where only 0o means octal. In C, a leading zero means octal, so 010 is
8. Python 3 rejects 010 in code and uses 0o10 instead, and JavaScript rejects it in strict mode.
What is base 36?
It's the largest base you can write with the digits 0 to 9 and the letters a to z. It makes short IDs that read the same in upper or lower
case, and JavaScript's
toString and parseInt accept bases up to 36 as well.