Every dayUnit Conversions

Number Base
converter.

Convert whole numbers between binary, octal, decimal, hex and any base up to 36.

How it's worked out

In any base, each digit is worth its own value times a place value. The place values are powers of the base, counted from 0 at the right. To read a number into decimal, multiply each digit by its place value and add:

N\displaystyle N=∑i=0k−1di×bi\displaystyle {}= \sum_{i=0}^{k-1} d_i \times b^{\,i}
  • bb is the base, for example 2, 8, 10 or 16.
  • did_i is the digit in position ii, counted from the right. Bases above 10 run out of numerals, so they use letters: A is 10, B is 11, and so on up to Z for 35.
  • kk is how many digits there are.

So FF in hex is 15 × 16 + 15 × 1, which is 255.

To write a number out in another base, divide by that base again and again. Each remainder is one digit. The first remainder is the last digit, so you read the remainders from the bottom up. The Working card shows these lines for your own number, then converts the answer back as a check.

Binary, octal and hex have a shortcut, because 8 is 2³ and 16 is 2⁴. Each octal digit is exactly 3 bits and each hex digit is exactly 4. Split the bits into groups from the right and swap each group for one digit: 1011 0110 becomes B6. The tool uses this method whenever both bases are 2, 8 or 16. It works with whole numbers of any length, so a 64-bit or 128-bit value converts with no rounding.

Worked examples

A homework question: 156 in binary

Maya's worksheet asks for 156 in binary, with working. Dividing by 2 eight times gives the remainders 0, 0, 1, 1, 1, 0, 0, 1. Read from the last one up, that is 1001 1100. To check, add the place values of the 1 bits: 128 + 16 + 8 + 4 = 156.

Open this example in the tool

A register value: 0x7FFFFFFF

Tom sees 0x7FFFFFFF in a debugger. In decimal it is 2,147,483,647, the largest signed 32-bit integer. Add 1 and you get 0x80000000. As a signed 32-bit number those bits mean −2,147,483,648. That is why a counter can jump from a huge positive value to a huge negative one.

Open this example in the tool

A negative number in one byte: −42

Computers store negative whole numbers in two's complement. To store −42 in 8 bits, write 42 as 0010 1010, flip every bit to get 1101 0101, then add 1. The result is 1101 0110, or 0xD6. In the tool, set the width to 8 bits and click the leftmost bit. The number jumps between −42 and 86.

Open this example in the tool

Reading your result

The big number is your value in the base you chose. Binary and hex come in groups of four digits and decimal has commas, so long values are easy to read. The line under it says the answer is exact and how many bits the number needs. It also shows how to type the number in code, with 0b for binary, 0o for octal or 0x for hex.

The tiles add the other common bases and the smallest standard type that holds the number. An unsigned 8-bit type (uint8) holds 0 to 255. A signed one (int8) holds −128 to 127. At 16 bits the limits are 65,535 unsigned, and −32,768 to 32,767 signed.

The bits card draws the number at 8, 16, 32 or 64 bits, with the hex digit under each group of four. The same bits can mean two numbers. Read as unsigned, 1111 1111 is 255. Read as signed, it is −1, because the leftmost bit counts as −128. The Unsigned and Signed switch flips between those two readings.

The common values table lists 0 to 16, the powers of 2 and the byte and word limits in your two bases. The list box converts a whole column at once. A line with a digit that does not belong to the base is marked as not valid instead of being guessed at.

Common mistakes

  • Reading the remainders top down. The first remainder is the last digit. Read top down, 156 comes out as 0011 1001, which is 57.
  • Grouping from the left. Bits are grouped from the right. 101 is one hex digit, 5, because the left group is padded with zeros to 0101.
  • Mixing up signed and unsigned. The pattern 1111 1111 is 255 as uint8 and −1 as int8. A file format or protocol spec says which one it means.
  • A leading zero in code. In C, and in older JavaScript, 010 is read as octal, which is 8. Write plain 8, or 0o10 in a language that allows it.
  • Reading chmod numbers as decimal. Permissions like 755 are octal. Each digit is 3 bits for read, write and run: 111 101 101.
  • Expecting fractions to convert neatly. 0.1 in decimal repeats forever in binary, which is why floating point numbers are slightly off. This tool converts whole numbers only, but of any length, well past 18,446,744,073,709,551,615.

Questions people ask

Use BASE(number, radix) to write a decimal number in any base from 2 to 36, and DECIMAL(text, radix) to read one back. The older DEC2BIN only handles −512 to 511, and HEX2DEC takes at most 10 hex digits (40 bits), reading a leading 8 to F as negative.

Sources

  • Knuth, D. E. The Art of Computer Programming, Volume 2, Section 4.1: Positional Number Systems
  • Patterson, D. A. and Hennessy, J. L. Computer Organization and Design, Section 2.4: Signed and Unsigned Numbers