How number bases work
A base is how many different digits you count with before you carry. Decimal (base 10) uses 0 to 9. Binary (base 2) uses 0 and 1. Octal (base 8) uses 0 to 7. Hexadecimal (base 16) uses 0 to 9 and then A to F for 10 to 15. Bases up to 36 carry on through the alphabet to Z.
Each place is worth the base times the place to its right. In decimal, 255 is 2 × 100 + 5 × 10 + 5 × 1. In hex, FF is 15 × 16 + 15 × 1, which is also 255. In binary, 11111111 is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1, again 255.
Why programmers use hex and binary
Computers work in bits, and long strings of 1s and 0s are hard to read. Each hex digit stands for exactly 4 bits, so a byte is always two hex digits and you can convert in your head a digit at a time: 1010 1111 is AF. Octal does the same with groups of 3 bits, which is why Unix file permissions are written in octal (see the chmod calculator).
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 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 |
Prefixes and separators
Code marks the base with a prefix: 0x for hex, 0b for binary and 0o for octal (some older languages use a leading 0 for octal, which is a common source of bugs). You can paste values with or without the prefix. Spaces, underscores and commas between digits are ignored, so 1111_0000, 1,048,576 and DE AD BE EF all work. In the Decimal box, a prefix switches the base for you, and assembly-style 0FFh and CSS-style #ff are read as hex.
Two’s complement and negative numbers
Computers store negative whole numbers using two’s complement. In a fixed number of bits, the top bit counts as a large negative value instead of a positive one. In 8 bits, that top bit is worth −128 instead of +128.
A worked example: −5 in 8 bits. Write 5 in binary, 0000 0101. Flip every bit to get 1111 1010, then add 1: 1111 1011, or 0xFB. Read the same bits as unsigned and you get 251. That’s why the same hex value can mean two different numbers.
Pick a width above to see the bit pattern, both readings and the bytes in big-endian and little-endian order. Auto picks the smallest of 8, 16, 32, 64 or 128 bits that holds the number. Here’s what each width holds:
| Bits | Unsigned range | Signed range |
|---|---|---|
| 8 | 0 to 255 | -128 to 127 |
| 16 | 0 to 65,535 | -32,768 to 32,767 |
| 32 | 0 to 4,294,967,295 | -2,147,483,648 to 2,147,483,647 |
| 64 | 0 to 18,446,744,073,709,551,615 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Big-endian and little-endian
A number bigger than one byte has to be stored as several bytes, and there are two orders to put them in. Big-endian puts the most significant byte first, the way you’d write it down; it’s used in network protocols. Little-endian puts the least significant byte first; it’s what x86 and most ARM processors use in memory. So the 32-bit value 0x12345678 sits in memory as 78 56 34 12 on a typical laptop.
Common mistakes
- Losing precision. JavaScript numbers, spreadsheets and many online tools round anything above 253. Hashes, IDs and 64-bit values need exact maths, which this converter uses.
- Reading signed as unsigned. If a value from a file or a network packet looks huge, it may be a small negative number. Pick the width and look at the signed reading.
- Leading-zero octal. In C and older JavaScript,
010is 8, not 10. - Mixing up 0 and O, 1 and l. In bases above 10, letters are digits. The converter reads letters in either case.
Need to encode bytes as text instead? Try Base64 encode and decode. Working out IP ranges in binary? The subnet calculator shows every address and mask in binary too.
Questions people ask
How do I convert binary to decimal?
1011 that’s 8 + 2 + 1 = 11. Type it in the Binary box above to check.How do I convert decimal to hex?
FF.What is two’s complement?
00000101, so −5 is 11111010 + 1 = 11111011. The top bit tells you the sign: 1 means negative when the value is read as signed.Why does 0xFF show as both 255 and −1?
11111111 is 255. As a signed 8-bit two’s complement number, the top bit is the sign, so it’s −1. Which one is right depends on the type in your code, such as uint8 or int8.