Number Base
converter.
Convert whole numbers between binary, octal, decimal, hex and any base up to 36.
255 needs 8 bits, so the smallest unsigned type is uint8. Read as a signed 8-bit number, the same bits mean −1, so a signed type needs int16.
The bits
Click a bit to flip it8-bit pattern 1111 1111. Read as unsigned it is 255; read as signed (two's complement) it is −1. The leftmost bit is 1, which is why the two readings differ.
Standard widths
| Width | Unsigned | Signed |
|---|---|---|
| 8-bit | Fits | No |
| 16-bit | Fits | Fits |
| 32-bit | Fits | Fits |
| 64-bit | Fits | Fits |
Working
- 1Divide by 2 again and again; each remainder is one digit255 ÷ 2 = 127 remainder 1127 ÷ 2 = 63 remainder 163 ÷ 2 = 31 remainder 131 ÷ 2 = 15 remainder 115 ÷ 2 = 7 remainder 17 ÷ 2 = 3 remainder 13 ÷ 2 = 1 remainder 11 ÷ 2 = 0 remainder 1
- 2Read the remainders from the last one up1111 1111₂
- 3Check: convert the answer back1111 1111₂ → 255₁₀
- 4Answer255₁₀ = 1111 1111₂
Common values, base 10 and base 2
| Base 10 | Base 2 |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 10 |
| 3 | 11 |
| 4 | 100 |
| 5 | 101 |
| 6 | 110 |
| 7 | 111 |
127, 255, 32,767 and 65,535 are the largest signed and unsigned 8-bit and 16-bit values.
Convert a list
Results
| Base 10 | Base 2 |
|---|---|
| 10 | 1010 |
| 100 | 110 0100 |
| 1000 | 11 1110 1000 |
| 1111 | 100 0101 0111 |
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:
- is the base, for example 2, 8, 10 or 16.
- is the digit in position , 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.
- 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.
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.
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.
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
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