Binary/Hexadecimal/Decimal Converter: a complete guide
Number bases in a nutshell
A number base, or radix, is how many different digits a counting system uses before it needs a new column. We normally count in base 10, with ten digits from 0 to 9. Computers store everything as on and off signals, so they work in base 2, called binary, which has only the digits 0 and 1.
Long binary strings are hard to read, so engineers and programmers also use base 16, called hexadecimal, where one hex digit stands for exactly four bits, and base 8, called octal, where one digit stands for three bits. Hex uses the digits 0 to 9 and the letters A to F for the values 10 to 15.
Converting by hand
To convert binary to decimal, give each digit a weight that doubles as you move left: 1, 2, 4, 8, 16 and so on. Add the weights where there is a 1. For 1101, that is 8 + 4 + 0 + 1, which is 13.
To convert decimal to binary, divide by 2 repeatedly and note the remainders, then read them from last to first. For 13: 13 divided by 2 is 6 remainder 1, 6 divided by 2 is 3 remainder 0, 3 divided by 2 is 1 remainder 1, and 1 divided by 2 is 0 remainder 1. Reading upwards gives 1101.
To go between binary and hex, group the bits in fours from the right. 1111 0101 is F5, because 1111 is F and 0101 is 5. The converter does all of this at once and also handles any base from 2 to 36.
Where you will meet these numbers
- Memory addresses and machine code are written in hex, for example 0x7FFF.
- Colour codes on the web, such as #FF8800, are three hex bytes for red, green and blue.
- Bit masks and flags in embedded programming, where each bit switches one feature on or off.
- Network work: IPv6 addresses are hex, and an IPv4 subnet mask such as 255.255.255.0 is easier to understand in binary.
- Digital electronics, PLC addresses and microcontroller registers.
- Permissions on Linux files are octal, where 755 and 644 are three octal digits.
Two's complement and negative numbers
Plain binary has no minus sign, so computers use two's complement to store negative whole numbers. The bits are the same, but the leftmost bit is given a negative weight. In an 8-bit number, the leftmost bit is worth -128, so 10000000 is -128 and 11111111 is -1.
To write a negative number such as -5 in 8 bits, take the positive value 00000101, flip every bit to get 11111010, and add 1 to get 11111011. The bits panel does this for you at 8, 16, 32 and 64 bits, and shows the unsigned and signed readings side by side. The same pattern of bits can mean 251 or -5 depending on whether it is read as unsigned or signed, which is a classic source of bugs.
Useful reference values
- 255 is FF in hex and 11111111 in binary, the largest 8-bit unsigned value.
- 256 is 100 in hex, and 1024 is 400 in hex.
- 65535 is FFFF, the largest 16-bit unsigned value.
- 127 is the largest signed 8-bit value, and -128 is the smallest.
- 2147483647 is 7FFFFFFF, the largest signed 32-bit value.