Hexadecimal

Hexadecimal is a way to represent a data based on 16 symbols:

0 - 9, A - F (16 in total)

The reason is:

  1. Easier to read hexadecimal comparing to a binary system
  2. Easier to remember hexadecimal
  3. Some times can combind to make meaning full word (i.e 0xc0010ff in MacOS when the computer is too hot — cool off)

Convert from Binary to Hexadecimal

To convert from binary to hexadecimal, we use the system of 8 4 2 1 for each 4 group of binary number. To calculate the hexadecimal representation, we simply add the corresponding value of 8 4 2 1 for bit that's 1

For example

8421Hexadecimal Representation
00000
00011
00102
00113
01004
01015
01106
01117
10008
10019
1010A
1011B
1100C
1101D
1110E
1111F

Note that to convert a binary, we can just sum the number of the toggled on bit (1) to see what's the hexadecimal representation.

For example, given 0110 we can see that bit 4 and 2 is 1. Therefore hexadecimal value is 0x6.

When convert a long binary value, for example 1100111010011010 into hexadecimal:

  1. we divide into group of 4: 1100 1110 1001 1010
  2. Convert them into letter based on the table above: C E 9 A
  3. Therefore the hexadecimal presentation is 0xCE9A

As a result, we can represent a long binary value to a much shorter value.

[!note]
In hexadecimal, we use the prefix 0x to denote that it's base 16 (is hexadecimal)

[!important]
Hexadecimals are often use to represent memory addresses in RAM or DISK because of this characteristic

General representation

Assume there are 5 heximal

01234
HexadecimalABC12
Base$16^4$$16^3$$16^2$$16^1$$16^0$

Therefore in decimal it would be: $10 \times 16^{4}+ 11 \times 16^{3}+ 12 \times 16^{2} + 1 \times 16^{1}+ 2 \times 16^0$ = 703506 (decimal — base 10 — normal number)