Nimble Toolbox

Convert a number between bases

Type a number and read it in binary, octal, decimal, and hex at once, plus any other base up to 36. A 0x, 0b, or 0o prefix sets the base for you, long values stay exact to the last digit, and negative numbers can be written in two's complement at 8, 16, 32, or 64 bits.

In each base
Read as
—
Binary
—
Octal
—
Decimal
—
Hex
—

How to use it#

  1. Type or paste a number. A 0x, 0b, or 0o prefix tells the page its base; without one, digits alone read as decimal and a to f as hex.
  2. Set "Read it as" when the number has no prefix and is in another base, such as binary written without 0b.
  3. Copy the row you need, or press "Add base" for a row in any base from 2 to 36. The row in the base you typed is marked "the given base". Pick a width under "Negative numbers" to see the bit pattern a signed integer holds.

How the page decides what base you typed#

A number written down does not say what base it is in. "1010" is a thousand and ten in decimal, ten in binary, and 4,112 in hex, and nothing in the digits settles which. So the page reads a prefix where there is one: 0x for hex, 0b for binary, and 0o for octal, the forms most programming languages use. Without a prefix, a number made only of the digits 0 to 9 is read as decimal, and one with any of the letters a to f is read as hex.

The "Read as" row says which reading it used and why. When that is wrong, as it is for binary or octal pasted without a prefix, set "Read it as" to the right base and the page reads every digit in that base instead. A digit the base does not have, such as a 9 in octal, stops the conversion and names the digit.

Spaces and underscores between digits are ignored, so 0b1010_1010 reads as 170, "1111 0000" reads as 240 with Binary chosen, and a decimal number may carry thousands commas such as "1,000,000". Letters can be either case.

Converting binary, decimal, and hex by hand#

Each position in a number is worth the base times the position to its right. In binary the positions are worth 1, 2, 4, 8, 16, and so on, so binary to decimal is adding up the positions holding a 1: 1101 is 8 + 4 + 1, which is 13. Decimal to binary runs the other way: divide by 2 again and again, write down each remainder, and read the remainders from last to first.

Hex to binary needs no arithmetic at all, which is why programmers use hex. Each hex digit stands for exactly four bits, so 0x3f is 0011 followed by 1111, and binary to hex is grouping the bits in fours from the right and writing each group as one digit. Octal works the same way in groups of three bits, which is where Unix file permissions such as 755 come from; the chmod calculator turns those into rwx letters.

Hexadecimal to decimal is the positional sum again with sixteens: 0x2a is 2 × 16 + 10, which is 42, and octal to decimal uses eights, so 0o17 is 8 + 7, which is 15. Decimal to hex is the repeated division again, by 16, with remainders of 10 to 15 written as a to f. Group the digits with the setting to check a long value by eye, in fours for binary and hex and threes for octal and decimal.

Negative numbers and two's complement#

Mathematically, negative 5 in binary is -101, and that is what the page writes with "Negative numbers" left on Minus sign. A computer stores it differently. A signed integer has a fixed number of bits, and in two's complement, the scheme every current processor uses, a negative value is stored as the bit pattern of that value plus 2 to the power of the width. In 8 bits, -5 is 256 - 5, which is 251, or 11111011.

Pick a width, and binary, octal, and hex show the stored pattern padded to the full width, the extra base shows the same pattern without padding, and decimal keeps the signed value. The "Unsigned" figure reads the same bits as an unsigned number. It also works the other way: with 32-bit chosen, pasting 0xffffffff gives -1, because a pattern whose top bit is set is read as negative. That is the usual errand when a debugger or a log prints a signed value in hex, and the reason a signed hex to decimal conversion needs a width at all.

Each width holds a fixed range. 8 bits hold -128 to 127, 16 bits -32,768 to 32,767, 32 bits about plus or minus 2.1 billion, and 64 bits about plus or minus 9.2 quintillion. A decimal number outside the range is refused with the range named, and a pattern longer than the width is refused with its length named, rather than either being wrapped around silently as a program would.

Long numbers stay exact#

JavaScript numbers lose precision past 2 to the power of 53, about 9 quadrillion, so a converter built on them turns a 64-bit ID or a hash value into something that ends in zeros. This page does its arithmetic on whole integers of any length, so a 20-digit decimal or a 256-bit hex value converts to the last digit, both ways. It takes numbers up to 4,096 characters long.

It converts whole numbers only. A fraction such as 0.1 often has no exact form in another base, and 0.1 in binary repeats forever, so a value with a point in it is turned away rather than rounded.

Frequently asked questions#

How do I convert binary to decimal?

#

Type the binary number with a 0b in front, or set "Read it as" to Binary and type the bits alone. The Decimal row shows the value, and the other rows show hex and octal at the same time. Without the 0b and with Auto chosen, a number made of 0s and 1s is read as decimal, since 1010 is a valid decimal number too.

Why is my number read as hex?

#

With "Read it as" on Auto and no prefix, any of the letters a to f among the digits makes the page read the number as hex, because hex is by far the most common base that uses letters. If the number is in another base that uses letters, such as base 32 or 36, choose that base under "Read it as".

How do I see a negative number's two's complement?

#

Choose a width under "Negative numbers": 8, 16, 32, or 64 bits. Binary, octal, and hex then show the bit pattern a signed integer of that width stores, padded to the full width, so -1 in 16 bits is 1111111111111111 or 0xffff. Pasting a pattern such as 0xff with 8 bits chosen reads it back as -1.

Why does 0xffffffff show as -1?

#

Because a width is chosen under "Negative numbers". At 32 bits, 0xffffffff has its top bit set, and two's complement reads such a pattern as negative: it is 4,294,967,295 minus 2 to the power of 32, which is -1. The "Unsigned" figure shows 4,294,967,295 for the same bits. Set "Negative numbers" back to Minus sign to read every hex value as positive.

Is base 32 here the same as Base32 encoding?

#

No. Base 32 on this page is a number written with the digits 0 to 9 and the letters a to v, the way hex uses 0 to f. Base32 encoding, as in RFC 4648, turns bytes into text with the alphabet A to Z and 2 to 7, padded with equals signs, and is a different thing. The same holds for base 64: Base64 is a byte encoding, which the Base64 encoder handles, and the largest number base this page writes is 36, with the digits 0 to 9 and a to z. Base 36 to decimal works like any other: set "Read it as" to Base 36.

What do 0x, 0b, and 0o mean?

#

They are prefixes that mark a number's base in source code: 0x for hex, 0b for binary, and 0o for octal, as in JavaScript, Python, and Rust. C and older code write octal with a bare leading zero instead, so 0755 means octal there; this page reads a leading zero as an ordinary digit, so set "Read it as" to Octal for numbers written that way. Tick "Write 0b, 0o, and 0x" to have the results carry their prefixes.

How large a number can it convert?

#

Up to 4,096 characters, which is far past any integer type, and the conversion is exact the whole way. Two's complement is limited to the width you pick, and a number too large for it is refused with a message naming the range or the length.