A number base is how many digits a number system counts with before it carries to
the next place. People use decimal, base 10. Computers use binary, base 2, and
programmers read it in hex, base 16, because one hex digit is exactly four bits. A
number base converter writes the same number in each of them. 255 is 11111111
in binary, 377 in octal and FF in hex.
A negative number in binary, octal and hex is written in two's complement at this width, like -1 as FF at 8 bits.
At 8 bits, 255 has its top bit set, so as a two's complement number it reads as -1.
Needs 8 bits. Runs in your browser. Nothing you type is uploaded.
Type into any box and the others update. Binary, octal, decimal and hex are always
there, and the Custom base box writes the number in any base from 2 to 36. A
box accepts the 0x, 0b and 0o prefixes for its own base. Pick a bit width
to choose how negative numbers are written in binary, octal and hex.
How to convert between bases
To decimal. Give each digit the value of the base to the power of its place, counting from 0 on the right, and add them up.
- Binary
1101is 1x8 + 1x4 + 0x2 + 1x1 = 13. - Hex
2Ais 2x16 + 10 = 42. - Octal
377is 3x64 + 7x8 + 7 = 255.
From decimal. Divide by the base, write down the remainder, and repeat with the
answer until you reach 0. The remainders, read from the last to the first, are the
digits. 13 in binary: 13 / 2 is 6 remainder 1, 6 / 2 is 3 remainder 0, 3 / 2 is 1
remainder 1, 1 / 2 is 0 remainder 1, which reads 1101.
Between binary and hex or octal. No arithmetic: group the bits in fours for hex
or threes for octal, and write each group as one digit. 1111 1111 is F F.
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 10 | 1010 | 12 | A |
| 42 | 101010 | 52 | 2A |
| 255 | 11111111 | 377 | FF |
| 256 | 100000000 | 400 | 100 |
| 1000 | 1111101000 | 1750 | 3E8 |
Negative numbers and two's complement
Decimal writes a minus sign. A computer has no minus sign to store, so it uses
two's complement: at a fixed width of N bits, a negative number is stored as
2^N plus the number. At 8 bits, -1 is 256 - 1 = 255, which is 11111111. -5
is 11111011, or FB in hex.
That is why the width matters. The same -1 is FF at 8 bits, FFFF at 16 bits
and FFFFFFFF at 32. Pick the width of the type you are working with, such as a
byte, a short, an int or a long, and type a negative number into the
Decimal box to see its bits. The range an N bit number holds is -2^(N-1) to
2^(N-1) - 1, so 8 bits hold -128 to 127. A number outside that is shown with a
minus sign, and a note says it needs a wider width.
Gotchas
- The box you type in decides how the digits are read.
10is ten in decimal, two in binary, eight in octal and sixteen in hex. Type into the box for the base your number is written in. - Hex and
0b. In the Hex box0b1is the numberB1, becausebis a hex digit, so only0xis a prefix there. The Binary box takes0b. - Letters are not case sensitive.
ff,FFandFfare the same hex number. The other boxes write upper case. - Leading zeros do not change the value.
0001and1are the same number. The converter does not pad the result to a byte or a word. - Exact beyond 2^53. JavaScript numbers are only exact up to
9007199254740992. The converter uses
BigInt, so large numbers and 64 bit values keep every digit. - Whole numbers only. There are no fractions or floating point here, only integers.
Related
- Binary Translator, for text as binary rather than numbers.
- ASCII Converter, which writes each character's code in decimal, hex, octal and binary.
- URL Encoder and Decoder, which writes bytes as hex percent-escapes.
- Hash Generator, whose digests are hex numbers.
- The Python cheat sheet for
bin,oct,hexandint(text, base). - The JavaScript cheat sheet for
toString(radix)andparseInt.
Common questions
How do I convert binary to decimal?
Type the binary number into the Binary box and the Decimal box shows the answer. By hand, give each digit a value of 2 to the power of its position, counting from 0 on the right, and add up the places that hold a 1. For 1101 that is 8 + 4 + 0 + 1 = 13.
How do I convert hex to decimal?
Type the hex number into the Hex box, with or without a 0x in front, and read the Decimal box. By hand, each hex digit is worth 16 to the power of its position, with A to F standing for 10 to 15. 2A is 2 x 16 + 10 = 42, and FF is 15 x 16 + 15 = 255.
How do I convert decimal to binary?
Type the number into the Decimal box and read the Binary box. By hand, divide by 2 again and again, writing down each remainder, until you reach 0. Then read the remainders from the last one back to the first. For 13 the remainders are 1, 0, 1, 1, so 13 is 1101 in binary.
What is a number base?
The base, or radix, is how many different digits a number system has before it carries to the next place. Decimal is base 10 with the digits 0 to 9. Binary is base 2 with 0 and 1, octal is base 8 with 0 to 7, and hexadecimal is base 16 with 0 to 9 and A to F. The same quantity can be written in any of them: 255 in decimal is 11111111 in binary, 377 in octal and FF in hex.
What does the custom base do?
It writes the number in any base from 2 to 36. Bases above 10 use the letters A to Z as digits 10 to 35, so base 36 uses all of 0 to 9 and A to Z. You can also type a number into the custom box to read it in that base. Base 36 is the one programmers use for short IDs, and zz in base 36 is 1295.
How are negative numbers shown in binary and hex?
In two's complement, the way computers store them. At a fixed width, a negative number is written as the pattern of 2 to the power of the width, plus the number. At 8 bits, -1 is 11111111 (FF) and -5 is 11111011 (FB). Choose 8, 16, 32 or 64 bits to match the type you are working with. Decimal keeps the minus sign, because that is how decimal is written.
Why does FF show as 255 and not -1?
Because a box reads what you type as an ordinary positive number. FF is 255 or -1 depending on whether the byte is unsigned or signed, and only the type can tell. When a pattern has its top bit set and fits the chosen width, the converter adds a note saying what it would be as a two's complement number, so FF at 8 bits is also -1. To get -1 as bits, type -1 into the Decimal box.
Does it work with very large numbers?
Yes, and exactly. The converter uses BigInt, which has no 53 bit limit like the plain numbers in JavaScript. 9007199254740993 comes out as 20000000000001 in hex, where a normal calculation would round it to ...992, and the largest 64 bit value, FFFFFFFFFFFFFFFF, is 18446744073709551615. It stops at 4,096 digits.
Can I type 0x, 0b or 0o in front of the number?
Yes. A 0x in the Hex box, a 0b in the Binary box and a 0o in the Octal box are accepted and dropped. Spaces, underscores and commas between digits are ignored too, so 1111 0000 and 1,000,000 read as you would expect. If you put the wrong prefix in a box, such as 0x in the Decimal box, the error says which box it belongs in.
Why are some digits marked in red?
Those characters are not digits in the base of the box you typed in. A 2 cannot be in a binary number, an 8 cannot be in an octal one and a G cannot be in hex. The converter marks each one and tells you which digits that base allows, rather than guessing what you meant.
Is anything I type here sent to a server?
No. The conversion runs in your browser and nothing is submitted anywhere. The site records that the tool was used, with which box and bit width, and how long the input was, so we know which tools are worth building next. The number itself is never part of that.