Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH

Hexadecimal Numbers

Hexadecimal numbers are numbers with sixteen possible values for each digit: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

What is a Hexadecimal Number?

A hexadecimal number uses digits with values 0 through 9, like in our normal decimal system, but uses values A through F in addition.

Press the buttons below to see how counting in hexadecimal numbers works:

Hexadecimal

{{ aValueHexadecimal }}

Decimal

{{ aValue }}

The term hexadecimal comes from the Latin 'hex', meaning 'six', and 'decimal', meaning 'ten', because this number system has sixteen possible digits.

The reason for using hexadecimal numbers is that they are more compact than decimal numbers, and easier to convert to and from binary numbers, since one hexadecimal digit corresponds exactly to four binary digits.

For example, the hexadecimal number 0 is 0000 in binary, and F is 1111 in binary numbers.

This means that writing three bytes (24 bits) in hexadecimal FF0000 takes only 6 characters, far easier than writing the same number in binary.

And writing #FF0000 is in fact a way to set the color red using RGB in CSS, with hexadecimal numbers.

Get an even deeper understanding of hexadecimal numbers by learning about binary numbers and bits and bytes as well.


Counting in Decimal Numbers

To better understand counting with hexadecimal numbers, it's a good idea to first understand the numbers we are used to: decimal numbers.

The decimal system has 10 different digits to choose from (0,..,9).

We start counting at the lowest value: 0.

Counting upwards from 0 looks like this: 1, 2, 3, 4, 5, 6, 7, 8, 9.

After counting up to 9, we have used up all the different values available to us in the decimal system, so we need to add a new digit 1 to the left, and we reset the rightmost digit to 0, we get 10.

A similar thing happens at 99. To count further, we need to add a new digit 1 to the left, and reset the existing digits to 0, we get 100.

Counting upwards, every time all possible combinations of digits have been used, we must add a new digit to continue counting. This is also true for counting using binary numbers and hexadecimal numbers.


Counting in Hexadecimal

Counting in hexadecimal is very similar to counting in decimal to start with: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

At this point in the decimal system, we have used up all the different digits available to us, but in the hexadecimal system, we have 6 more possible digits, so we can keep counting!

A

B

C

D

E

F

At this point, we have used up all the different digits available to us in the hexadecimal system, so we need to add a new digit 1 to the left, and reset the existing digit to 0, we get 10 (which is equal to the decimal number 16).

We continue counting, using two digits:

10

11

..

...

1F

20

21

...

FF

It happened again! We have used up all the different possibilities with two digits, so we need to add another new digit 1 to the left, and reset the existing digits to 0, we get 100, which is equal to the decimal number 256.

This is similar to what happens in decimal when we count from 99 to 100.

Understanding hexadecimal numbers becomes a lot easier if you're able to see the similarities between counting in hexadecimal and counting in decimal and binary.


Decimal Values

To understand how hexadecimal numbers are converted to decimal numbers, it's a good idea to first see how decimal numbers get their value in the base 10 decimal system.

The decimal number 374 has 3 hundreds, 7 tens, and 4 ones, right?

We can write this as:

\[ \begin{equation} \begin{aligned} 374 {} & = 3 \cdot \underline{10^2} + 7 \cdot \underline{10^1} + 4 \cdot \underline{10^0} \\[8pt] & = 3 \cdot \underline{100} + 7 \cdot \underline{10} + 4 \cdot \underline{1} \\[8pt] & = 300 + 70 + 4 \\[8pt] & = 374 \end{aligned} \end{equation} \]

The math above helps us better understand how hexadecimal numbers are converted to decimal numbers.

Notice how \(10\) appears three times in the first line of calculation?

\[ 374 = 3 \cdot \underline{10}^2 + 7 \cdot \underline{10}^1 + 4 \cdot \underline{10}^0 \]

That is because \(10\) is the basis of the decimal number system. Each decimal digit is a multiple of \(10\), and that is why it is called a base 10 number system.


Converting Hexadecimal to Decimal

When converting from hexadecimal to decimal, we multiply the digits by powers of 16 (instead of powers of 10).

Let's convert the hexadecimal number 3C to decimal:

\[ \begin{equation} \begin{aligned} 3C {} & = 3 \cdot \underline{16^1} + 12 \cdot \underline{16^0} \\[8pt] & = 3 \cdot \underline{16} + 12 \cdot \underline{1} \\[8pt] & = 48 + 12 \\[8pt] & = 60 \end{aligned} \end{equation} \]

In the first line of calculation, each hexadecimal digit gets multiplied by 16 in the power of the digit's position.

The first position is 0, starting from the rightmost digit. That is why C, which is equal to 12, is multiplied by \(16^0\) since C's position is 0.

The fact that each hexadecimal digit is a multiple of 16 is why it is called a base 16 number system.

The calculation above shows that the hexadecimal number 3C is equal to the decimal number 60.

Click the individual hexadecimal digits below to see how other hexadecimal numbers are converted to decimal numbers:

Hexadecimal

Decimal

{{ digitToHex(digit) }}

{{ aValueDecimal }}

Calculation

{{aValueHexadecimal}}

 = 

 = 

 = 

 = 

As you can see, each hexadecimal digit is a multiple of 16, 16 in the power of the digit's position.


Converting Decimal to Hexadecimal

To convert a decimal number to a hexadecimal number, we can divide by 16, repeatedly, while keeping track of the remainders.

Let's convert the decimal number 42 to hexadecimal:

\[ \begin{aligned} 42 \div 16 &= 2,\ \text{remainder } \underline{10} \\[8pt] 2 \div 16 &= 0,\ \text{remainder } \underline{2} \\[8pt] \end{aligned} \]

Reading the remainders from bottom to top, with 10 being A in hexadecimal, we get that 2A is the hexadecimal representation of 42.

Click the individual decimal digits below to see how other decimal numbers are converted to hexadecimal numbers:

Decimal

Hexadecimal

{{ digit }}

{{ aValueHexadecimal }}

Calculation

Result: 


It is important to note why we use hexadecimal numbers.

Hexadecimal numbers are used because they are a more compact representation of binary numbers, which means they are easier to write and read.

For example, the value of B is easier to write and read than the value of 1011.

Furthermore, each hexadecimal digit maps exactly to 4 binary digits, meaning there are 16 possible values for a hexadecimal digit, just like there are 16 possible values for a group of 4 binary digits. So that for example a one byte (8-bit) value can be represented exactlyby 2 hexadecimal digits.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.