See all glossary termsWe are used to count in a decimal system.
This is a system where the amount of unique numerals is ten, starting at 0
until 9
. To count more than the tenth number (which is 9), we need to use 2 or more of the existing numerals.
In decimal, the digits are
0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
.
We call this a system with a "base 10"
To reach the number after 9
, we need to associate two previous numerals: 0
, and 1
: 10
, 11
, ..., 42
, ...
Hexadecimal is a "base 16" system, where the digits are 0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
, A
, B
, C
, D
, E
, F
.
Decimal | Hexadecimal |
---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
10 | A |
11 | B |
12 | C |
13 | D |
14 | E |
15 | F |
If we want to represent 16 in decimal, we need to add two more digits: 1
, and 0
. It's very unintuitive! But you get used to it.
Decimal | Hexadecimal |
---|
16 | 10 |
17 | 11 |
18 | 12 |
19 | 13 |
20 | 14 |
... | ... |
160 | A0 |
161 | A1 |
176 | B0 |
- Split the number in digits
- For each digit, convert it do decimal (
A
to 10
, B
to 11
, C
to 12
, and so on)
- Counting from the right, multiply each digit by the 16 raised to the power of its position. So the first digit is 16^0, the second digit is 16^1, and so on.
- Add all the products together
digit | power 16 | | |
---|
C | 12 X 16^1 | = | 192 |
2 | 2 X 16^0 | = | 2 |
| total | = | 194 |
digit | power 16 | | |
---|
F | 15 X 16^1 | = | 15 |
F | 15 X 16^0 | = | 240 |
| total | = | 255 |
In regular programming, hexadecimal is most often encountered to encode colors.
#FF0000
is red.
#00FF00
is green.
#0000FF
is blue.
The format is #RRGGBB
, where RR
, GG
, and BB
are hexadecimal values for the red, green, and blue channels, respectively.
This is because the hexadecimal color encoding takes two digits to represent each color channel: in the first example,FF
is the amount of red in the color, and 00
is the amount of green in the color. FF
is equal to 255
, so that is equivalent to writing rgb(255, 0, 0)
.
Some hexadecimal notations add two digits to represent alpha: #RRGGBBAA
, where 00
is completely transparent, and FF
is completely opaque.
Finally, in some languages, it is accepted to use the #RGB
format, forgeting the 0
for small numbers. school.gdquest.com - 0.3.5-2024-12-12T18:17:18.104Z-a38dc74be3238fd1ea161f4df67b95520741da9d-release