Understanding Hexadecimal Numbers
The average person deals with one number system – base 10. They don’t call it that though. To most people, they’re just numbers. Most developers, at some point, will need to deal with three number systems – base 2 (binary), base 10 (decimal), and base 16 (hexadecimal). The emphasis of this discussion is on hexadecimal numbers, but decimal and binary are used for comparison.
In a number system, the term base refers to the number of individual values it uses. Decimal, which is base 10, contains 10 values – the numbers 0 thru 9. Binary has 2 values – 0 and 1. Hexadecimal has 16 values – 0 thru 9, A thru F. Here’s a table of comparing number 0 through 15.
Decimal | Binary | Hex
--------------------------------------------
0 | 0 | 0
1 | 1 | 1
2 | 10 | 2
3 | 11 | 3
4 | 100 | 4
5 | 101 | 5
6 | 110 | 6
7 | 111 | 7
8 | 1000 | 8
9 | 1001 | 9
10 | 1010 | a
11 | 1011 | b
12 | 1100 | c
13 | 1101 | d
14 | 1110 | e
15 | 1111 | f
One thing you’ll notice is how the higher the base, the narrower the numbers. One digit in hex, can represent up to 4 digits in binary. That makes hex much more efficient number system for people to write out or remember. The
Decimal | Binary | Hex
---------+-----------------------+----------
1 | 1 | 1
2 | 10 | 2
4 | 100 | 4
8 | 1000 | 8
16 | 10000 | 10
32 | 100000 | 20
64 | 1000000 | 40
128 | 10000000 | 80
256 | 100000000 | 100
512 | 1000000000 | 200
1024 | 10000000000 | 400
2048 | 100000000000 | 800
4096 | 1000000000000 | 1000
8192 | 10000000000000 | 2000
16384 | 100000000000000 | 4000
32768 | 1000000000000000 | 8000
65536 | 10000000000000000 | 10000
131072 | 100000000000000000 | 20000
262144 | 1000000000000000000 | 40000
524288 | 10000000000000000000 | 80000
1048576 | 100000000000000000000 | 100000
When Will I Use This?
Hexadecimal numbers have a few popular places where they get used, in the web development world. We’ll describe each of them below. Discussing the conversion of decimal to hexadecimal or decimal to binary can be done another time. For now, lets just talk about what they are and how they get used.
CSS Colors
A web developer will recognize hexadecimal numbers from RGB colors in CSS. They’re usually written as a 6-digit value, in the form of #RRGGBB where each pair of digits represents Red (#FF0000), Green(#00FF00), and Blue (#0000FF) respectively, with values ranging from 00-FF (0-255).
Git Hashes
Git 40-character SHA1 hashes. While Git is creating these hashes for you, it’s their usage here is important. Can you imagine how many digits you’d have to use for a Git SHA1 hash if the say was in decimal? or even worse, binary?! That number would be huge!
GUID
GUIDs are Global Universal Identifiers. The Ramsey/UUID library in PHP makes use of them. These values are sometimes used in databases for uniqueness, instead of auto-incrementing primary keys.
URL-Encoding
In URL, some characters that have special meaning. We use url-encoding encoded to prevent misinterpretation of the content. This encoding uses ‘%’ followed by hexadecimal digits that represent it’s corresponding ASCII value. For example, a space becomes %20 because 20 is its hexadecimal value in the ASCII table.