Anyone who spends enough time with a computer or other digital device will likely come across mysterious records consisting of numbers and Latin letters, such as . For people not familiar with it, they may seem like some cipher. What is behind these symbols? It turns out that these are simply numbers in HEX (Hexadecimal Number System). HEX is an alternative way to represent numbers.
HEX can also be written in lowercase (hex, an example of usage: Wikipedia article), but in this topic, we, for consistency, will use only uppercase HEX or, simply, hexadecimal.
Why HEX-a-decimal?
For years before the invention of computers, people used the decimal (base-) system for counting – because it's convenient when you have fingers and toes.
In computers, however, there are only two options: on and off, which created the idea of a binary digit or bit, for short.
For convenience, computer engineers tended to group bits. In the 1960s, they could group bits at a time: a group of bits is the base a base- number.
As computers got more powerful, people learned to cluster bits by instead of . A group of bits, written as one symbol, can have values instead of – in other words, two times more than a group of bits. So, this system is more compact than decimal, octal (base-), and binary systems!
The only thing missing – a name for the new excellent number system.
A single number can have values.
Hex (from Greek) means , and decimal (from Latin, but that's OK) means (also, we are already used to the decimal number system and the word decimal). So… HEX-a-decimal looks like the perfect name!
Hexadecimal digits
The hexadecimal number system uses numbers and the first six Latin letters: (corresponding to decimals ), as hexadecimal digits.
Yes, in some number systems, letters can be digits, too!
The table below compares how the numbers are written in the binary, the decimal, and the HEX number systems:
Binary | Decimal | HEX |
|---|---|---|
So, as you see, we start using letters when we run out of digits in HEX. How is the decimal number represented in HEX? The answer is !
It's better to write numbers with their base as a lower index so you don't confuse decimal with hexadecimal :
You might have already noticed those subscripts in the table above. Other ways to avoid confusion include writing a HEX number with h after it or 0x before it. For instance, 63h, 0x63 and are three different ways to indicate that is a hexadecimal number.
Binary to HEX and vice versa
HEX is widely used in computer science because we can easily translate binary numbers into HEX. Moreover, HEX is a simplified way to represent binary numbers.
Remember: is . That means we can split (starting from the right or, in other words, from the end) any binary number into a sequence of -digit binary numbers. These numbers are then mapped to HEX digits using the table above.
Let's try converting the number to HEX:
Of course, everything is not as simple as it seems. If the length of our binary number is not divisible by without remainder, then the leftmost number of our sequence will contain less than digits:
Did you notice that in a decimal system, adding any number of zeroes to the left of a decimal number (leading zeroes) doesn't change the number's value? , and so on. Well, not only the decimal number system works like that:
In any base-n (n > 1) number system, adding any number of leading zeroes to the number does not change its value. Those zeroes are even called non-significant zeroes.
How can it help us? Remember that , and, therefore:
So, if the leftmost number in our split-sequence of -digit binary numbers contain less than digits, we add leading zeroes to it until it is a -digit binary number and then match it, along with the rest in the sequence, to the corresponding HEX digit using the table. The resulting sequence is ready to be mapped to HEX digits using the table above.
Converting a hexadecimal number into a binary is even easier. Each HEX digit corresponds to a -digit binary sequence. All you have to do is to map digits to sequences with the table.
If you need to convert a number from decimal to hexadecimal, you must divide it by multiple times during the calculation. The binary to hexadecimal conversion method is computationally simple and dividing by is easier than dividing by . Therefore, it is reasonable to convert in two steps using the conversion to binary number. Thus, to convert a number from decimal to hexadecimal, convert the decimal number to binary and then the resulting binary number to hexadecimal using the method above. For example, we convert the number to binary and get , and then convert to hexadecimal and get . To convert back from hexadecimal to decimal, use the same idea. To achieve that, convert the hexadecimal number to binary and the resulting number to decimal.
Applications of HEX
We use the hexadecimal system to store error codes during the work of various software products. For example, in some operating system errors are encoded in this way. If you decode your error code, you'll find out the exact error that occurred. Besides, in URLs, character codes are written as hexadecimal pairs prefixed with %. You can see for yourself by googling a symbol @. The link in the address bar of your browser would look like this:
https://www.google.com/search?q=%40HEX numbers are also used for writing programs in low-level languages and in some encodings. For example, Unicode (a computer standard for symbol encoding) represents every symbol as a hexadecimal number. HEX numbers encode even color schemes. Thus, in RGB encoding, every color can be defined as pairs of hexadecimal numbers, each for the Red, Green, or Blue color components, respectively.
Conclusion
As you can see, there are many ways to use HEX. In this topic, we have covered that:
A HEX number is basically a -bit group with values that can include numbers from to and as hex-digits;
Converting a HEX number into a binary requires splitting into a sequence of -digit binary numbers;
In the real world, the application of HEX numbers is vast — from error codes to symbol encoding.
We hope that this information will be helpful to you in the future. But for now, let's turn to practice!