What Is Hexadecimal? Base 16 and Hex to Decimal Explained
Hexadecimal is a base 16 number system using digits 0 to 9 and letters A to F. Learn how to convert hexadecimal to decimal and why programmers use hex.
Updated
Hexadecimal, or hex, is a base 16 number system. It uses sixteen digits: 0 to 9, then the letters A to F for the values ten to fifteen. Each position is worth sixteen times the one to its right, so hex 2F means 2 sixteens plus 15 ones, which is 47 in decimal.
The sixteen hex digits
Decimal runs out of digits at 9 and carries into a new column. Hex keeps going for six more values before it carries, borrowing letters to stand in for them. Upper and lower case mean the same thing, so ff and FF are equal.
| Hex | Decimal | Binary | Hex | Decimal | Binary |
|---|---|---|---|---|---|
0 | 0 | 0000 | 8 | 8 | 1000 |
1 | 1 | 0001 | 9 | 9 | 1001 |
2 | 2 | 0010 | A | 10 | 1010 |
3 | 3 | 0011 | B | 11 | 1011 |
4 | 4 | 0100 | C | 12 | 1100 |
5 | 5 | 0101 | D | 13 | 1101 |
6 | 6 | 0110 | E | 14 | 1110 |
7 | 7 | 0111 | F | 15 | 1111 |
After F comes 10, which is sixteen. Counting continues 11, 12 up to 1F (thirty-one), then 20 (thirty-two).
Hexadecimal to decimal
Each position in a hex number is a power of 16. From the right, the place values are 1, 16, 256, 4,096, 65,536 and so on.
- Write the place values under the digits, starting with 1 on the right.
- Replace any letters with their values: A is 10, B is 11, C is 12, D is 13, E is 14, F is 15.
- Multiply each digit by its place value.
- Add the results.
Worked example: convert 7E9.
- 7 × 256 = 1,792
- E is 14, and 14 × 16 = 224
- 9 × 1 = 9
Adding them gives 1,792 + 224 + 9 = 2,025.
Two smaller ones to check your understanding: FF is 15 × 16 + 15 = 255, and 1A3 is 256 + 160 + 3 = 419.
Decimal to hexadecimal
Go the other way by dividing by 16 and collecting remainders, the same idea as the divide by 2 method in Decimal to Binary.
For 2,025: dividing by 16 gives 126 with remainder 9. Dividing 126 by 16 gives 7 with remainder 14, which is E. Dividing 7 by 16 gives 0 with remainder 7. Read the remainders from last to first: 7E9.
Hex and binary fit together
Sixteen is 2 to the power 4, which means one hex digit covers exactly four bits. That is the real reason hex exists. To convert binary to hex, split the bits into groups of four from the right and swap each group for its digit from the table above.
11010110 splits into 1101 and 0110. Those are D and 6, so the byte is D6. Going back is just as mechanical: 3B is 0011 then 1011.
A byte is eight bits, so it is always exactly two hex digits, from 00 to FF. A 32-bit value is eight hex digits. Compare that with decimal, where a byte takes one to three digits and you cannot see the bits at all. For the full method of turning bits into decimal values, see Binary to Decimal.
Why programmers use hex
Hex is the standard shorthand for raw binary data. The places you are most likely to meet it:
- Colors. A web color such as
#FF6600is three bytes: redFF(255), green66(102) and blue00(0). The guide to HEX, RGB and HSL color codes goes through this in detail, and the Color Converter translates between the formats. - Memory addresses. Debuggers and crash reports print addresses such as
0x7FFE4A10. Memory is laid out in power of two blocks, so boundaries that look arbitrary in decimal are round numbers in hex: 4,096 is0x1000and 65,536 is0x10000. - Byte dumps and file formats. Hex editors show each byte as two digits. Every PNG file starts with the bytes
89 50 4E 47. - Network identifiers. A MAC address is six bytes written as twelve hex digits, and IPv6 addresses are written as groups of four hex digits.
- Hashes and IDs. SHA-256 digests and UUIDs are long binary values printed in hex.
- Character codes. Unicode code points are written in hex, so the letter A is
U+0041.
How to tell a number is hex
Because 10 could be ten or sixteen, hex numbers are usually marked.
| Marker | Where it is used | Example |
|---|---|---|
0x prefix | C, C++, Java, JavaScript, Python and most languages | 0x1F |
# prefix | CSS and design tools, for colors | #1F2937 |
h suffix | Some assembly languages | 1Fh |
&#x prefix | HTML character references | A |
| Subscript 16 | Math textbooks | 1F with a small 16 |
Using the converter
The Number Base Converter keeps hex, decimal, binary and octal fields in sync. Type into the hexadecimal field and the decimal value updates as you type, with the binary form grouped in fours so you can see each hex digit's bits. Enter the digits only, without a 0x or # in front, and in either case. Hex output is shown in capitals.
The converter works with whole numbers up to 32 bits, so the largest hex value it takes is FFFFFFFF, which is 4,294,967,295. That covers bytes, color codes and 32-bit addresses. A full 64-bit address is too long for it, so convert those in two halves or by hand with the place value method.
If you are new to bits and bytes, How to Read Binary starts from the beginning.
Try it
Type 7E9 into the hex field of the Number Base Converter and confirm the decimal reads 2025. Then enter FF, 100 and FFFF and watch where the binary digits roll over. More converters are in the numbers category.