If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Hexadecimal numbers

Binary numbers are a great way for computers to represent numbers. They're not as great for humans though—they're so very long, and it takes a while to count up all those 1s and 0s. When computer scientists deal with numbers, they often use either the decimal system or the hexadecimal system. Yes, another number system!
Fortunately, number systems are more alike than they are different, and now that you've mastered decimal and binary, hexadecimal will hopefully make sense.
In the decimal system, each digit represents a power of 10—the ones' place, the tens' place, etc. We call 10 the base of the decimal system.
Check your understanding
What is the base of the binary system?
Each digit represents a power of
  • Your answer should be
  • an integer, like 6
  • a simplified proper fraction, like 3/5
  • a simplified improper fraction, like 7/4
  • a mixed number, like 1 3/4
  • an exact decimal, like 0.75
  • a multiple of pi, like 12 pi or 2/3 pi

In the hexadecimal system, each digit represents a power of 16.
Here's how to count to 10 in hexadecimal: 1, 2, 3, 4, 5, 6, 7, 8, 9, A.
That looks very familiar until the final "number" A. You see, in the hexadecimal system, each digit needs to represent the values 0-15, but the decimal numbers 10-15 don't fit into a single digit. To work around that, the hexadecimal system uses the letters A-F to represent the numbers 10-15.
Here, let's count from 10 to 15: A, B, C, D, E, F. Strange but true!
There's actually been many different proposals over the years for how to represent the values 10-15, but A-F is the solution that won out.
Let's count higher now. Here's the decimal number 24, represented in hexadecimal as 18:
18
161
161160
This number requires two digits, where the right digit represent the ones' place (160) and the left digit represents the sixteens' place (161). The same way that we added decimal and binary numbers, we can add up (1×16)+(8×1) and see that hexadecimal 18 equals the decimal value 24.
Let's try a number with a letter in it. Here's the decimal number 27, represented in hexadecimal as 1B:
1B
161
161160
To understand this one, we need to first remember that B represents the value 11. I do that by counting the letters on my fingers, but you can count however you'd like, as long as you remember to start with A representing 10.
Then we can add it up like before and see that (1×16)+(11×1) does equal the decimal value 27.
Check your understanding
Let's see if this is making sense. What decimal value is represented by the hexadecimal number 1F?
  • Your answer should be
  • an integer, like 6
  • a simplified proper fraction, like 3/5
  • a simplified improper fraction, like 7/4
  • a mixed number, like 1 3/4
  • an exact decimal, like 0.75
  • a multiple of pi, like 12 pi or 2/3 pi

Why base 16?

At this point, you might be wondering what it is that computer scientists like so much about the hexadecimal system. Why use a system where we have to use letters to represent numbers? A little detour into history will show us why...
Early computers used 4-bit architectures, which meant that they always processed bits in groups of 4. That's why we still write bits in groups of 4, like when we write 0111 to represent decimal 7 even though we could just write 111.
How many values can 4 bits represent? The lowest value is 0 (all 0s, 0000) and the highest value is 15 (all 1s, 1111), so 4 bits can represent 16 unique values. Aha, there's that number 16!
Each group of 4 bits in binary is a single digit in the hexadecimal system. That makes it really easy to convert binary numbers to hexadecimal numbers, and it makes it a natural fit for computers to use as well.

Converting binary to hexadecimal

Let's prove how well binary and hexadecimal get along by converting from binary to hexadecimal.
We'll start with a short binary number:
0010
That's 4 bits long, which means it maps to a single hexadecimal digit. There's a 0 in every place except the twos' place, so it equals the decimal number 2. The decimal and hexadecimal systems both represent the numbers 0-9 the same way, so 0010 is simply 2 in hexadecimal.
Let's try a longer binary number:
1001101001101100
One approach would be to figure out what decimal number is represented by that long string of 1s and 0s, and then convert that to hexadecimal. That approach would work, but gosh, it seems like a lot of work for this long of a number.
The easier approach is to convert each group of 4 bits, one at a time.
Starting with the left-most group of 1001, that equals (1×8)+(1×1), the decimal number 9. That number is less than 10, so 1001 is simply 9 in hexadecimal.
The next group is 1010, or (1×8)+(1×2), the decimal value 10. That number requires a letter representation in hexadecimal, A.
The next group is 0110, or (1×4)+(1×2), the decimal value 6. That number is the same in hexadecimal, 6.
The final group is 1100, or (1×8)+(1×4), the decimal value 12. That number requires a letter representation in hexadecimal, C.
Our final hexadecimal result is 9A6C. We did that conversion without ever understanding what decimal number is represented. I'll reveal now that both 1001101001101100 and 9A6C equal the decimal number 39,532. That's an awfully big number, I'm glad we converted it one digit at a time.
Check your understanding
Now you try it. What hexadecimal number does this binary number represent?
1011100100011000
Choose 1 answer:

Patterns in hexadecimal numbers

Let's build some intuition around the hexadecimal system.
First: for a given number of digits, what does the biggest number look like for those digits? In the decimal system, that's all 9s, like 9,999. In the binary system, that's all 1s, like 1111.
In the hexadecimal system, that's when every digit has an F in it, FFFF. When we see a number like that, we know that it represents the highest possible value for that number of digits. To represent any higher number, we'd need more digits.
Okay, now how much does a big number like that represent? We can use the same rule that we used for binary: the largest number that can be represented by a number of digits n is the same as 16n1. Now that we're dealing with base 16 instead of base 2, we might need to bring out a calculator to figure that out though.
Here's a table of the first four largest values:
DigitsHighest hexadecimal numberDecimal equivalent
1F15(1611)
2FF255(1621)
3FFF4095(1631)
4FFFF65535(1641)
Check your understanding
The table shows the highest values for each number of digits. What is the total number of unique values that can be represented by 2 digits?
  • Your answer should be
  • an integer, like 6
  • a simplified proper fraction, like 3/5
  • a simplified improper fraction, like 7/4
  • a mixed number, like 1 3/4
  • an exact decimal, like 0.75
  • a multiple of pi, like 12 pi or 2/3 pi

Uses for hexadecimal

In this unit on how computers work, we're mostly going to be dealing with binary numbers. It's important to understand the hexadecimal system as well though, because they'll pop up throughout later units and just generally, in the life of a programmer.
As one example from my life, web developers use hexadecimal numbers to represent colors. We describe colors as a combination of three components: red, green, and blue. Each of those components can vary from 0 to 255. A color like blue can be written as rgb(0, 0, 255) or the more concise hexadecimal version, #0000FF. Using that notation, we can describe 166 unique colors—more than 16 million colors!
Now that you know about hexadecimal numbers, keep your eyes out for them. You'll often see them written with an 0x in front, like 0x4F, or you might just recognize their distinctive mix of 0-9 with A-F. If you spot any interesting uses for them in the wild, share your discovery below in the Tips & Thanks.

🙋🏽🙋🏻‍♀️🙋🏿‍♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!

Want to join the conversation?

  • aqualine ultimate style avatar for user sebastian nielsen
    I quote the article: "Now that you know about hexadecimal numbers, keep your eyes out for them. You'll often see them written with an 0x in front, like 0x4F"

    Why are they often written with 0x in front?
    (39 votes)
    Default Khan Academy avatar avatar for user
  • spunky sam blue style avatar for user 𝘉𝘭𝘶𝘦𝘣𝘪𝘳𝘥
    I've seen base 64 used quite a bit as well, Why 64?

    I suspect it has something to do with 64 being a power of 2, like 16 is a power of 2. Any thoughts?
    (17 votes)
    Default Khan Academy avatar avatar for user
    • leaf grey style avatar for user Alex
      Mainly due to 64 being a power of 2. I would also guess that because it just so happens that the total number of uppercase and lowercase letters and numbers is 62, we use base64 for text encoding.
      (16 votes)
  • leafers tree style avatar for user makoto.57
    "#0000FF. Using that notation, we can describe 16^6 unique colors..."

    Why is #000000 16^6 instead of 16^5? We start from 16^0, not 16^1.
    (5 votes)
    Default Khan Academy avatar avatar for user
    • male robot donald style avatar for user Levos
      I think it's easier to understand if we go back to binary a little:

      With one bit you can represent two values (0 and 1). What happens if we add a second bit? For each value of this second bit, the previous bit can still be 0 or 1, so we essentially multiply the number of unique values by 2.
      1 bit => 0 or 1

      2 bits with the new bit being 0 => 00 or 01
      2 bits with the new bit being 1 => 10 or 11
      So in this case it's 2 * 2 = 4.

      All of this is true for other number systems. In my example above we multiply by 2 because we are using the binary system.

      Now in the hexadecimal system for the same multiplication we use 16. As one hexadecimal digit can represent 16 unique values, two digits can represent 16 * 16 = 256.

      The general formula looks like this:
      unique values = b^n,
      where b is the base of the number system and n is the number of digits. What matters is the number of digits, not the the exponent of the base on the leftmost place.

      Hope this helps!
      (19 votes)
  • blobby green style avatar for user Jackie Liu
    why is the total number of unique values that can be represented by 2 digits in hexadecimal FF = 256 instead of 272 (256 + 16)?
    (5 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user langat chebet
    how do i work out 9C36 to normal decimal numbers
    (3 votes)
    Default Khan Academy avatar avatar for user
    • leaf green style avatar for user Shane McGookey
      Hexadecimal numbers are base-16, whereas the decimal numbers that we are accustomed to are base-10. (9C36)_16 (using _16 to represent base-16) is equivalent to (39990)_10 (using _10 to represent base-10).

      You can determine this result through the following process:

      Pick apart each of the digits of the hexadecimal number, so that you have (starting at the last digit and going towards the first) 6 3 C 9. Recall that the character 'C' represents the number 12 in hexadecimal, so you have 6 3 12 9.

      Now, multiply by powers of 16, starting from 0:

      6 * 16^0 = 6 * 1 = 6
      3 * 16^1 = 3 * 16 = 48
      12 * 16^2 = 12 * 256 = 3072
      9 * 16^3 = 9 * 4096 = 36864

      Adding the results together... 6 + 48 + 3072 + 36864 = 39990.
      This conversion process would hold regardless of how long the hexadecimal number is, just continue multiplying by incremental powers of 16 and then sum the results.
      (13 votes)
  • blobby green style avatar for user LiuYecheng
    What is said of the color in the computer?
    Can you explain?
    (1 vote)
    Default Khan Academy avatar avatar for user
    • aqualine ultimate style avatar for user Martin
      Colors can be encoded in hexadecimal, it's called the RGB encoding.
      Imagine you have 3 lights: one red (R), one green(G) and one blue(B).
      No each of those lights have switch with 256 (0 to 255) settings, where 0 means the light is off and 255 or FF in hexadecimal is the light is running on full power.

      So we start by turning the lights all off
      00 00 00 that means we get black
      No we turn the green light all the on, so to FF in hexadecimal(255)
      00 FF 00 that means we get a bright green
      Now, because we paid attention in art class we turn on the red and the green light each to 150 (or 96 in hexadecimal) to get purple
      96 00 96 that's some kind of purple
      And the end we turn all three lights all the way on
      FF FF FF and we get white
      (13 votes)
  • duskpin tree style avatar for user kishoregunadala
    The table shows the highest values for each number of digits. What is the total number of unique values that can be represented by 8 digits?
    (3 votes)
    Default Khan Academy avatar avatar for user
    • duskpin tree style avatar for user WRaven
      16^8 - 1, or 4,294,967,295.

      The highest value that can be expressed increases exponentialy for each digit you add.
      For any number of digits, just take the base you are in B, to the power of the number of digits D, and then subtract 1.
      B^D - 1

      Example: Base 12, 7 digits
      12^7 - 1
      35831808 - 1
      35831807
      (4 votes)
  • duskpin seedling style avatar for user Lucy van Pelt
    Are the English alphabet letters in hexadecimal used universally?
    (5 votes)
    Default Khan Academy avatar avatar for user
  • male robot hal style avatar for user geethaaluri
    i want to do coding not binary
    (1 vote)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user Jason Thompson
    How would you get 34 in hexadecimal in decimal form
    (2 votes)
    Default Khan Academy avatar avatar for user
    • blobby green style avatar for user kaylaeca
      Hexadecimal numbers are base-16, whereas the decimal numbers that we are accustomed to are base-10. (34)_16 (using _16 to represent base-16) is equivalent to (52)_10 (using _10 to represent base-10).

      You can determine this result through the following process:

      Pick apart each of the digits of the hexadecimal number, so that you have (starting at the last digit and going towards the first) 4 3.

      Now, multiply by powers of 16, starting from 0:

      4 * 16^0 = 4 * 1 = 4
      3 * 16^1 = 3 * 16 = 48

      Adding the results together... 4 + 48 = 52.
      This conversion process would hold regardless of how long the hexadecimal number is, just continue multiplying by incremental powers of 16 and then sum the results.
      (4 votes)