Main content

## Computers and the Internet

### Course: Computers and the Internet > Unit 1

Lesson 3: Hexadecimal numbers# 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.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, start text, A, end text.

That looks very familiar until the final "number" start text, A, end text. 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 start text, A, end text-start text, F, end text to represent the numbers 10-15.

Here, let's count from 10 to 15: start text, A, end text, start text, B, end text, start text, C, end text, start text, D, end text, start text, E, end text, start text, F, end text. Strange but true!

There's actually been many different proposals over the years for how to represent the values 10-15, but start text, A, end text-start text, F, end text is the solution that won out.

Let's count higher now. Here's the decimal number 24, represented in hexadecimal as 18:

1 | 8 |
---|---|

16 | 1 |

16, start superscript, 1, end superscript | 16, start superscript, 0, end superscript |

This number requires two digits, where the right digit represent the ones' place (16, start superscript, 0, end superscript) and the left digit represents the sixteens' place (16, start superscript, 1, end superscript). The same way that we added decimal and binary numbers, we can add up left parenthesis, 1, times, 16, right parenthesis, plus, left parenthesis, 8, times, 1, right parenthesis 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 1, start text, B, end text:

1 | start text, B, end text |
---|---|

16 | 1 |

16, start superscript, 1, end superscript | 16, start superscript, 0, end superscript |

To understand this one, we need to first remember that start text, B, end text 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 start text, A, end text representing 10.

Then we can add it up like before and see that left parenthesis, 1, times, 16, right parenthesis, plus, left parenthesis, 11, times, 1, right parenthesis does equal the decimal value 27.

### 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:

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:

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 left parenthesis, 1, times, 8, right parenthesis, plus, left parenthesis, 1, times, 1, right parenthesis, the decimal number 9. That number is less than 10, so 1001 is simply 9 in hexadecimal.

The next group is 1010, or left parenthesis, 1, times, 8, right parenthesis, plus, left parenthesis, 1, times, 2, right parenthesis, the decimal value 10. That number requires a letter representation in hexadecimal, start text, A, end text.

The next group is 0110, or left parenthesis, 1, times, 4, right parenthesis, plus, left parenthesis, 1, times, 2, right parenthesis, the decimal value 6. That number is the same in hexadecimal, 6.

The final group is 1100, or left parenthesis, 1, times, 8, right parenthesis, plus, left parenthesis, 1, times, 4, right parenthesis, the decimal value 12. That number requires a letter representation in hexadecimal, start text, C, end text.

Our final hexadecimal result is 9, start text, A, end text, 6, start text, C, end text. We did that conversion without ever understanding what decimal number is represented. I'll reveal now that both 1001101001101100 and 9, start text, A, end text, 6, start text, C, end text equal the decimal number 39, comma, 532. That's an awfully big number, I'm glad we converted it one digit at a time.

#### 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, comma, 999. In the binary system, that's all 1s, like 1111.

In the hexadecimal system, that's when every digit has an start text, F, end text in it, start text, F, F, F, F, end text. 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 16, start superscript, n, end superscript, minus, 1. 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:

Digits | Highest hexadecimal number | Decimal equivalent |
---|---|---|

1 | start text, F, end text | 15, left parenthesis, 16, start superscript, 1, end superscript, minus, 1, right parenthesis |

2 | start text, F, F, end text | 255, left parenthesis, 16, squared, minus, 1, right parenthesis |

3 | start text, F, F, F, end text | 4095, left parenthesis, 16, cubed, minus, 1, right parenthesis |

4 | start text, F, F, F, F, end text | 65535, left parenthesis, 16, start superscript, 4, end superscript, minus, 1, right parenthesis |

#### 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 16, start superscript, 6, end superscript 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?

- 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?(36 votes)- thats a general label that means the following numbers and letters are hexadecimal, in the development of big softwares its important to label things properly as to not get confused between one programmer and the next(56 votes)

- 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?(16 votes)- 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.(13 votes)

- 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)
- Because there is no character to represent 16. Hexadecimal characters go from 0 to F (15). Since the F represents 15 the number FF is 255 because (15*16) + (15) = 255.(16 votes)

*"#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.(4 votes)- 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!(16 votes)

- how do i work out 9C36 to normal decimal numbers(3 votes)
- 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.(12 votes)

- What is said of the color in the computer?

Can you explain?(1 vote)- 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(9 votes)

- Help! I am stuck in hexadecimal numbers.

Can anyone help me.

It is complicating.(5 votes) - Are the English alphabet letters in hexadecimal used universally?(4 votes)
- Why is there no character for 16, if it is base 16?(1 vote)
- Including 0, numbers 0 - 15 are 16 numbers total.

Its the same reason why 0-9 is decimal (meaning 10) even though 9 is the highest number.(6 votes)

- How would you get 34 in hexadecimal in decimal form(2 votes)
- 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.(3 votes)