What a positional number system is
In the decimal number 4,095 the 4 is not worth 4: it is worth 4,000, because it sits in the thousands place. Each position multiplies its digit by a power of 10 (1, 10, 100, 1,000...). That is a positional system, and the base is how many different digits it uses: ten in decimal.
Binary works the same way with base 2: only 0 and 1 exist, and each place is worth double the previous one. Hexadecimal uses base 16: its sixteen digits are 0 to 9 and the letters A to F, and each place is worth 16 times the previous one. Computers work in binary because a circuit only has to tell two states apart (off and on), and hexadecimal is a short way to write those bits.
| Position (right to left) | Value in binary | Value in hexadecimal |
|---|---|---|
| 1st | 1 | 1 |
| 2nd | 2 | 16 |
| 3rd | 4 | 256 |
| 4th | 8 | 4,096 |
| 5th | 16 | 65,536 |
| 6th | 32 | 1,048,576 |
| 7th | 64 | 16,777,216 |
| 8th | 128 | 268,435,456 |
Binary to decimal, by hand
Write each digit with the value of its place and add up only the places that hold a 1. Example with 1101: from right to left the places are worth 1, 2, 4 and 8.
- Write the number and, under it, the value of each position: 1 1 0 1 above 8 4 2 1.
- Check each place: there is a 1 in the 8 place, a 1 in the 4 place, a 0 in the 2 place and a 1 in the 1 place.
- Ignore the places that hold a 0, here the 2.
- Add: 8 + 4 + 0 + 1 = 13. So binary 1101 is 13 in decimal.
With more digits it is the same: 11111111 is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, the largest value that fits in 8 bits. And 10110110 is 128 + 32 + 16 + 4 + 2 = 182.
Decimal to binary, by repeated division
Divide the number by 2 again and again, writing down the remainder of each division (always 0 or 1), until the quotient is 0. The remainders, read from bottom to top, are the binary number. Example with 45:
- 45 ÷ 2 = 22, remainder 1
- 22 ÷ 2 = 11, remainder 0
- 11 ÷ 2 = 5, remainder 1
- 5 ÷ 2 = 2, remainder 1
- 2 ÷ 2 = 1, remainder 0
- 1 ÷ 2 = 0, remainder 1
Reading the remainders from bottom to top gives 101101. To check it, add: 32 + 8 + 4 + 1 = 45.
There is an alternative many people prefer: subtract powers of 2 from largest to smallest. For 200: 128 fits (72 left), 64 fits (8 left), 32 and 16 do not fit, but 8 does (0 left). The places used are 128, 64 and 8, so 200 = 11001000.
Hexadecimal: sixteen digits and a table to keep handy
Since there are no single digits for 10 to 15, the letters A, B, C, D, E and F are used. One hexadecimal digit equals exactly four bits (a "nibble"), which is why it is so convenient: a byte, which is 8 bits, is written with just two hexadecimal digits.
To go from hexadecimal to decimal, multiply each digit by the power of 16 of its place and add. For example, 3F = 3 × 16 + 15 = 63 and A3 = 10 × 16 + 3 = 163.
| Decimal | Binary (4 bits) | Octal | Hexadecimal |
|---|---|---|---|
| 0 | 0000 | 0 | 0 |
| 1 | 0001 | 1 | 1 |
| 2 | 0010 | 2 | 2 |
| 3 | 0011 | 3 | 3 |
| 4 | 0100 | 4 | 4 |
| 5 | 0101 | 5 | 5 |
| 6 | 0110 | 6 | 6 |
| 7 | 0111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | A |
| 11 | 1011 | 13 | B |
| 12 | 1100 | 14 | C |
| 13 | 1101 | 15 | D |
| 14 | 1110 | 16 | E |
| 15 | 1111 | 17 | F |
Why hexadecimal is used: bytes, colors and addresses
A byte is 8 bits and runs from 0 to 255 (00000000 to 11111111). Writing bits by hand is slow and error-prone; in hexadecimal the same byte is two characters, 00 to FF. To go from binary to hexadecimal, split the bits into groups of four starting from the right and replace each group with its digit from the table. Example: 11010111 splits into 1101 and 0111, which are D and 7, so it is D7. In decimal that is 13 × 16 + 7 = 215.
The best-known case is web colors. In #FF8800 there are three bytes: FF for red (255), 88 for green (136) and 00 for blue (0). It is a strong orange: all the red, a bit more than half the green and no blue. Hexadecimal also shows up in memory addresses, error codes and network identifiers.
From text to binary with ASCII
A computer stores each letter as a number, and that number as bits. In the ASCII table A is 65, which in 8-bit binary is 01000001. The word "Hi" is two letters: H is 72 (01001000) and i is 105 (01101001), so it is written 01001000 01101001. "Hola" is 72, 111, 108 and 97, that is 01001000 01101111 01101100 01100001, or 48 6F 6C 61 in hexadecimal.
The tool uses UTF-8 encoding: basic English letters take one 8-bit byte each, but an ñ, an accented letter or an emoji takes several bytes, so they give more than 8 bits. For example, ñ is two bytes: 11000011 10110001 (C3 B1 in hexadecimal).
How to do all this with the tool
The "Binary, Decimal and Hex" converter is good for checking your hand calculations or for solving long numbers without mistakes.
- Type the number in the field for its base: "Decimal (base 10)", "Binary (base 2)", octal or "Hexadecimal (base 16)". The other three fill in on their own.
- If a digit does not belong to the base (a 2 in binary, for example), the tool warns you.
- For text, type it in the "Text" field and look at "Binary (8 bits per character)" and the hexadecimal bytes. You can also paste bits in groups of 8 to get the text back.
- For unusual bases, use "Any base (2 to 36)": pick the source base, the target base and the number.
The limits: it only converts non-negative whole numbers (no decimals or sign) and it does not add leading zeros for you, so if you want a fixed 8 bits you have to pad by hand (13 is 00001101). Nothing you type leaves your browser.
Common mistakes
- Reading the remainders backwards: in repeated division the first remainder is the rightmost digit, not the leftmost.
- Dropping leading zeros when they matter: 13 is 1101, but as a byte it is 00001101. It matters when the length is part of the data, as in a color or a network mask.
- Mixing up the letter O with zero, or hexadecimal B (11) with an 8, when copying. In hexadecimal letters mean the same in upper and lower case, but they are not the same as a digit.
- Leaving the 0x prefix or the # sign in when converting: they only mark the number as hexadecimal and should be removed before pasting.
- Thinking binary 10 is ten: it is two. The same "10" is worth 2, 10 or 16 depending on the base.
Worked practice problems
Try each one before looking at the answer, then check with the tool.
- Convert 10110110 to decimal and hexadecimal. Answer: 128 + 32 + 16 + 4 + 2 = 182; in groups of four, 1011 0110 = B6.
- Convert 200 to binary and hexadecimal. Answer: 11001000 and C8 (12 × 16 + 8 = 200).
- Convert 7F to binary. Answer: 7 = 0111 and F = 1111, so 01111111, which is 127 in decimal.
- Write "OK" in binary. Answer: O is 79 and K is 75, that is 01001111 01001011.
Frequently asked questions
How do I convert 1101 from binary to decimal?
Add the values of the places that hold a 1: 8 + 4 + 1 = 13. The second digit from the right is a 0, so the 2 is not added.
What is 255 in binary and in hexadecimal?
255 is 11111111 in binary and FF in hexadecimal. It is the largest value that fits in an 8-bit byte.
Why does hexadecimal use letters?
Because it needs sixteen different symbols and the decimal system only provides ten (0 to 9). So A is 10, B is 11, C is 12, D is 13, E is 14 and F is 15.
How many different numbers can you write with 8 bits?
256, from 0 to 255. In general, n bits can represent 2 to the power of n values.
Does the tool convert negative or decimal numbers?
No, only non-negative whole numbers. For text it uses UTF-8, so accented letters and emojis give more than 8 bits.