Hexadecimal Numbers Refresher

Yves Boutellier
CodeX
Published in
6 min readSep 20, 2021

Have you ever encountered something like U+61 or 0xFF or #FFAA33 and asked yourself what those letters and numbers in combination mean? Recently, I was learning about unicode. Unicode has assigned numbers to each symbol in nearly all languages that humans ever created (of course only those that are well documented). Those numbers are most often represented as hexadecimal numbers (for example U+61).

I wanted to feel more confident if I am seeing those numbers. Therefore I decided to revise my shaky understanding of hexadecimal numbers. I wanted to have an intuition how large those numbers are. Additionally I wanted to be able to translate from one base into another. Let’s say from hexadecimal to decimal.

Since I believe in sharing my learning path via blog articles, I summarised my learnings regarding the hexadecimal system for you. From time to time I posted questions, the solutions are at the bottom of this short article 😊

In another article, I am probably going to write about my experience with unicode.

What is a number system

Hexadecimal is like any other number or counting system. The systems differ mathematically regarding their base. For example, you are familiar with the number system that has basis of 10. This systems has 10 numbers (0 through 9) that make up all numbers that possibly exist.

You start counting 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and then you use a combination of the numbers that came before 10, 11, 12 and so forth.

Hexadecimal has a base of 16. But which letters or numbers should we use to count up to 16?

People agreed to use letters A through F. However, in the 1950s and 1960s many other symbols, letters and numbers were suggested. For example some old computers used u, v, w, x, y and z.

Photo by Ugi K. on Unsplash

But let us count in hexadecimal once

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 00, 01, 02, 03 …, 09, 0A, 0B, 0C, 0D, 0E, 0F, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, ... 1F, 20

Okay, do you think you got it? Which number follows 19F ? Easy? What is the previous number to 100 ?

Why is hexadecimal widely used in programming?

It has practical reasons. In contrast to bit-numbered counting system (like 1011), hexadecimal is more readable to humans. But it has at the same time the advantage that hexadecimal numbers allow a quick translation to the bit wise number representation. That is to say it is easier to translate from hex code to binary instead of 10-base numbers to binary, while still readable to humans.

Let’s see how we translate from binary to hex. In hexadecimal we have 16 different symbols (base 16). Half a byte (a nibble) with 4 bits can represent 16 different symbols.

This means each byte can be represented by two hexadecimal symbols. We can split up a byte into two nibbles. A byte can go from

0 0 0 0 0 0 0 0 to 1 1 1 1 1 1 1 1 .

In hexadecimal we can write a byte can go from 00 to FF. This is easier than writing 8 digits or using 256 different numbers.

by author

Translation from Hexadecimal to Decimal

I already showed you how you count in hexadecimal and how hexadecimal numbers represent nibbles. I am sure you know what F stands for. 15 exactly. But what is FFF in decimal?

Translation from hexadecimal to decimal is in the beginning maybe unclear and escapes itself from your intuition. If you tried it before and left it frustrated, I think I can get you to the point where you can derive the formula yourself.

Let’s start with just one symbol. A,A comes after 9, thus is 10. That was easy.

What about F01? Easy? I compute this example for you. 3841.

How does it work?

It’s very similar for decimal, no big deal!

How would you decompose a decimal number like 2984? Maybe you remember from secondary or elementary school.

10^3 * 2 + 10^2 * 9 + 10^1 * 8 + 10^0 * 4 = 2000 + 900 + 80 + 4 = 2984

And it’s not very different from translating decimal numbers into hexadecimal. We can follow the same logic to make us familiar with hexadecimal. Hexadecimal is no magic, but it perturbs a field of human reasoning that is so naturally to us, that for some of us, it’s hard to think outside of the 10 based system. The example from before F01 evaluates to

16^2 * 15 + 16^1 * 0 + 16^0 * 1 = 3841

by author

Translation from Decimal to Hexadecimal

Let us translate 3841 back to F01

We know that we have to find factors that are powers of 16 times a constant. 16^3 = 4096 is larger than 3841, thus is not part of the decomposition. But how many times does 16^2=256 fit in 3841? 15 times (=F)! Remainder is 1.

16 (16^1) does not fit in 1. Only 1 does fit in 1. 16^0 = 1. Thus the result is F01 .

Let’s do a second example.

783 in hexadecimal?

  1. 783 // 256 = 3
  2. 783 % 256 = 15
  3. 15 // 16 = 0
  4. 15 % 16 = 15
  5. 15 // 1 = 15 = F

This makes 30F. I hope you feel confident enough. Try this one yourself. Solution is at the end of the article: 6020 in hex?

In what form do you encounter hexadecimal numbers out in the wild?

From my experience, hexadecimal numbers are in the beginning hard to detect, since many conventions exist and each convention decided to write hexadecimal numbers differently. I show you a few different examples, that are very common and used in practice

  • Unix, C and C descendant programming languages use the prefix 0x, ie. FF (255) becomes 0xFF.
  • Character and strings may express hexadecimal with the prefix \x, 1B becomes \x1B
  • In Unicode the prefix U+ is used. E.g. U+122
  • Colour references in HTML or CSS are expressed with 6 hexadecimal digits. Two for each RGB colour, they use a # as prefix. #FFAA33 is golden orange.

Summary

Hexadecimal numbers are somehow different to regular decimal numbers. That is what you were thinking before you read this article. Now, after finishing you are hopefully realising that it’s very similar to regular decimal number system. The comparison during the section covering how to translate from hex to decimal made this perhaps clear to you. You hopefully acknowledge you the wide use of hexadecimal numbers and you can detect them in any popular naming convention out there.

Solutions:

The number after 19F is 1A0

Previous number to 100 is FF

Base 10 / Base 16

6020 = 1784

At any time you can test yourself here:

--

--