Hexadecimal Number Value
In one sentence: Hexadecimal counts in sixteens using the digits 0 to 9 and the letters A to F, and this formula turns a hex number into the ordinary number it stands for.
Read it out loud: “N equals the sum, for every position i from 0 up to n minus 1, of d sub i times 16 to the power of i. N is the ordinary decimal number. n is how many hex digits there are. i is the position of a digit, counted from zero on the right. d sub i is the digit in position i, a value from 0 to 15. The 16 with i as a small raised number means sixteen multiplied by itself i times. The sigma sign means add up all the terms.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- A number system and its base
A number system is a way of writing numbers with a fixed set of digit symbols. The base is how many different digit symbols it uses. Our everyday system is base 10, with the digits 0 to 9. Binary is base 2, with digits 0 and 1. Hexadecimal is base 16, with sixteen digit symbols: 0 to 9 and then A to F. Each place in a number is worth the base raised to a power.
- Powers of sixteen, like
A power is a number multiplied by itself a set number of times, and the small raised number, the exponent, says how many times. So and . Any number to the power 0 is 1, so . The places of a hex number are 1, 16, 256, 4096 and so on, and each place is sixteen times the place on its right.
- Letters used as digits, A to F
Hexadecimal needs sixteen digit symbols, so after 0 to 9 it borrows the letters A to F. These letters are digits, not words. A means 10, B means 11, C means 12, D means 13, E means 14 and F means 15. So the hex digit F is worth fifteen, not six, and the hex number 1A is one sixteen plus ten ones, which is 26.
- Nibbles, bytes and bits
A bit is one 0 or 1. A nibble is a group of four bits, and a byte is a group of eight bits, which is two nibbles. One hex digit stands for exactly one nibble, so two hex digits stand for exactly one byte. This is the main reason hexadecimal is used for computer data.
- The 0x prefix
Programmers often start a hex number with the two characters 0x, as in 0x2F. The 0x is a label that says the digits are hexadecimal. It is not one of the digits and it is not counted in the value, so 0x2F and 2F are the same number.
- The sum sign and the position counter
The Greek capital letter sigma, , means add up a list of terms. The counter starts at 0 under the sign and stops at above it, so it takes each position in turn. The formula writes the same thing as one term for each digit, with a plus sign between them.
What each symbol means
| Symbol | Meaning |
|---|---|
| The ordinary decimal number that the hex digits stand for. It is a plain count with no unit. The hex number 2F stands for , which means forty-seven. | |
| How many hex digits the number has. It is a count with no unit. The number 2F has two digits, so , and a number written with eight hex digits has . | |
| The position of one digit, counted from 0 on the right. Position 0 is the rightmost digit. It has no unit, and for a two-digit number the positions are 0 and 1. | |
| The digit in position , a value from 0 to 15 with no unit. The symbols 0 to 9 are worth 0 to 9, and the letters A, B, C, D, E and F are worth 10, 11, 12, 13, 14 and 15. | |
| The place value of position : how much one unit in that place is worth. It has no unit. The place values are 1, 16, 256, 4096 and so on, and each one is sixteen times the place on its right. | |
| The sum sign, which means add up the terms for every position from 0 up to . It has no unit. In plain words, go through every digit from the right to the left, work out its value and add all the values together. |
The formula piece by piece
The place value of one position. The base is sixteen, so the place values are 1 on the right, then 16, then 256, then 4096 for each place further left. Each place is worth sixteen times the place on its right.
The digit in that same position, from 0 to 15. It is written as 0 to 9 or as the letters A to F, and the letter is converted to its value first. A digit F in a place counts as fifteen units of that place.
Multiply the digit by its place value. A digit of 2 in the sixteens place is worth , and a digit F in the ones place is worth . Each digit gives its full worth for its place.
Add those products for every digit position, from position 0 on the right up to position on the left. The total is the ordinary number that the hex digits stand for.
The whole formula in one sentence: the ordinary number N equals the total you get by adding, for every digit position, that digit multiplied by its place value in base sixteen.
What the formula is doing, and why
What goes in and what comes out. You give the formula a hex number, written with the digits 0 to 9 and the letters A to F. It gives back the ordinary decimal number. For the hex number 2F the answer is 47. The formula reads every digit, multiplies it by the place value of its position, and adds the results.
Why sixteen? Each place is worth sixteen times the place on its right, so the places are 1, 16, 256 and 4096. Sixteen equals , a power of two, and that is why each hex digit lines up with exactly four bits. Hex is therefore a short way of writing binary, four bits at a time.
Why do the digits run up to 15? Each digit in a base-16 place can be any value from 0 to 15, which is sixteen possible values, so sixteen digit symbols are needed. The letters A to F cover the values 10 to 15. The largest number with hex digits is . For two digits that is , which is why one byte fits in exactly two hex digits, from 00 to FF.
When it applies. The formula is for numbers written in base sixteen. The 0x prefix in front of a number, such as 0x2F, tells you the digits are hexadecimal, but the prefix is not part of the value. Hex is used because it is much shorter than binary, and converting between hex and binary is quick, because each digit maps to four bits.
An everyday picture
Think of an odometer whose wheels carry sixteen marks, the digits 0 to 9 and then A to F, instead of ten. When a wheel passes F, it goes back to 0 and moves the wheel on its left forward by one step, in the same way that a decimal odometer carries after 9. Each wheel is worth sixteen times the wheel on its right.
Worked example: Reading the web color #FF8800
A web page sets a color with the hex code #FF8800. The # sign tells the browser that a color code follows, and it is not part of the value. The six digits split into three pairs, one for each light channel: red is FF, green is 88 and blue is 00. Each pair has two hex digits, which is one byte, so each channel runs from 0 to 255.
- Split the code into pairs. The six digits are , and . Each pair has two hex digits, and each pair stands for one byte. We read each pair with the formula, using the place values 16 on the left and 1 on the right.
- Read the red pair FF. The left F is in the 16 place and is worth . The right F is in the 1 place and is worth . Adding them gives , the largest value a byte can hold, which means red at full strength.
- Read the green pair 88. Each 8 stands for eight units. The left 8 is in the 16 place and is worth , and the right 8 is in the 1 place and is worth 8. Adding gives . In binary, 88 is , which is the same 136. Green is a little over half of full strength.
- Read the blue pair 00. Both digits are 0, so each product is and . The total is 0, which means no blue light at all. A zero digit adds nothing, whatever its place.
- Read the whole six-digit code as one number. Treated as one number, FF8800 has six positions, and the top pair FF sits at places and . Its value is . A program may store the color as this single number, and it is read back by splitting it into the same three pairs.
- Check the largest pair. The largest pair is FF, which is , and the smallest is 00, which is 0. Every channel of a web color therefore has 256 possible strengths, from 0 to 255. That is exactly one byte per channel.
Answer: The color #FF8800 has red 255, green 136 and blue 0, which is a bright orange. Read as one ordinary number, the six hex digits stand for 16,746,496.
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Treating the letters as special symbols that cannot be added. means 10 and means 15, so is , not 11.
- Using 10 as the place value instead of 16. The number is , not .
- Counting the letters from the wrong starting point. is 10, so is 11, is 12, is 13, is 14 and is 15. Do not read as 6.
- Using a symbol that is not a hex digit, such as . Hex digits run from 0 to F, so is not a valid digit.
- Mixing up the base. The digits mean sixteen in hex and ten in decimal. Write the base when it is unclear, as in .
Where you will meet it
- Web colors, written as #RRGGBB, where each pair of hex digits is one byte from 00 to FF. For example, #FF0000 is pure red.
- MAC addresses, which are 48 bits long and are written as six pairs of hex digits, for example 00:1A:2B:3C:4D:5E.
- Memory addresses in debuggers and crash logs, which often show values such as 0x7FFF.
- Unicode code points, the numbers that identify each character. The letter é, for example, has the code point U+00E9.
Questions people ask
Why do programmers use hex instead of binary?
Hex is shorter. One hex digit stands for four bits, so a 32-bit number fits in eight hex digits, while writing it in binary takes 32 digits. Hex also converts to and from binary one digit at a time.
Why base 16 and not base 10?
Sixteen is , a power of two, so each hex digit lines up with exactly four bits. Base ten does not line up with binary in that way.
How do I convert hex to decimal?
Multiply each digit by its place value, which is a power of 16, and add the results. For example, gives .
Related formulas
- Binary Number Value: Binary uses only the digits 0 and 1, and this formula turns a row of those digits into the ordinary number they stand for.
- Two's Complement: Two’s complement is the usual way computers write negative whole numbers in binary, and this formula reads a bit pattern as a positive or negative number.
- Floating-Point Value: A floating-point number stores a value as a sign, a power of two and a binary fraction, and this formula turns those three parts back into the number.
- Bits Needed: This formula counts the fewest bits you need so that every one of N items gets its own pattern of 0s and 1s.