⌚ September 3, 2014
Computers love strings of bits, such as 10001001 01100111 10101110, but they can be difficult for humans to read and write accurately.
To facilitate readability and conserve space when viewed in a monitor or in print, we use the hexadecimal (base 16) system as a shorthand to represent binary.
Hexadecimal, or hex, is a base 16 number system that contains sixteen digits.
0 1 2 3 4 5 6 7 8 9 A B C D E F
For digits 0 through 9, we use the same numbers as we do in the decimal system, but that only provides digits for the first ten values. We need six more, so we use the first six letters from the English alphabet to represent the hexadecimal values 10 through 15.
Base 16 relies upon a 4bit system because 16 unique combinations of bit patterns are possible. 2^4 = 16. Each bit pattern corresponds to exactly one 4bit pattern.
Binary Hex Value Decimal  0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15
To convert from binary to hexadecimal, divide the binary bit pattern, from right to left, into 4bit groups, and replace each group with its corresponding hex value. Pad with leading zeros if necessary to aid readability.
Convert Binary 10101110 into Hex
This has eight bits, so split the byte into two 4bit groups. Keep the bit ordering the same.
1010 1110
Replace each group with its hex value.
1010 1110 A E
AE is the hex equivalent of binary 10101110.
Convert Binary 110011 to Base 16
From right to left, split 110011 into two 4bit groups.
11 0011
If a group has fewer than four bits, pad it with leading zeros to its left. Leading zeros do not alter the value.
11 > 0011 0011
Replace each 4bit group with the corresponding hex value.
0011 0011 3 3
33 is the hex equivalent of binary 110011.
Denoting Hexadecimal
In the previous example, how do we know that 33 is hexadecimal and not 33 in base 10? The two have different values even though they look the same, so we need a way to differentiate them. There are four methods used to label a number as hexadecimal in order to avoid ambiguity.
1. Place 0x before the hex numeral: 0x33 2. Append a lowercase letter h after the numeral: 33h 3. Use the 16 subscript: 33_{16} 4. Specify base 16: 33 (base 16)
The 0x notation is the most common. Use one 0x for each hex grouping. 0x33, 0xF, 0xFFEE, are 0xAB66CD90 are all valid examples. However, do not prefix 0x separately to each digit, such as 0x3 0x3 to represent hex 33. Use 0x33.
Capitalization
Uppercase and lowercase hex digits have the same meaning.
0xAB56 = 0xab56 0x9F = 0x9f 0x33b = 0x33B
The x in 0x may also be capitalized, but this has no effect on the value of the hex number either.
0xBC342A = 0XBC342A 0xbc342a = 0Xbc342a
In practice, the lowercase x in 0x is more common and more readable.
Consistency
Be consistent with the chosen capitalization. Do not mix uppercase and lowercase letters together within the same hex grouping. For example,
Write 0xAB3C or 0xab3c, not 0xaB3C. Write 0xCC, not 0xcC or 0xCc. Write 0x2b3f4d, not 0x2B3f4d.
Converting Hexadecimal to Binary
Each hex digit represents one 4bit group, so replace each hex digit with its binary value. Keep the order of the bits the same as the order of the hex digits.
Convert 0x34 to binary
3 = 0011 4 = 0100
Consult the hexadecimal table above. For 0x34, 3 is replaced with 0011 and 4 is replaced with 0100. Maintain the order.
0x34 > 0011 0100
0x34 = 00110100 binary
Convert 0xBC5 to binary
B = 1011 C = 1100 5 = 0101
Case does not matter, so B and C are the same as b and c. Replace the hex digits with their binary values in the same order of appearance.
0xBC5 = 1011 1100 0101 = 101111000101 base 2
Converting Hex to Decimal
First, convert from hex to binary, and then convert the binary to decimal.
Convert 45h to decimal
4 = 0100 5 = 0101
Once we have converted 45h to binary 0100 0101, we perform the same binarytodecimal conversion as described in Lesson 4.
45h = 01000101 (binary) = 64 + 4 + 1 = 69 (decimal)
Convert c38_{16} to decimal
The subscript 16 in c38_{16} specifies base 16, so we know that c38 is a hexadecimal value.
c = 1100 3 = 0011 8 = 1000
c38_{16} = 1100 0011 1000_{2} = 3,128_{10}
The subscript 2 in indicates base 2 and the subscript 10 indicates base 10.
Readability
For bit strings containing more than eight bits, recognition is easier by preserving the spaces between nibbles or bytes. The place values still increment across the spaces.
0001 1101 1100 1100 is easier to read than 0001110111001100.
10011001 11110000 10100000 11110001 is slightly easier to read than 10011001111100001010000011110001.
Memorizing the Hex Digits
For faster mental calculations, we can memorize the sixteen hexadecimal values, their bit patterns, and their decimal equivalents. This is easier than it sounds.
The Extremes
The two extremes, all zeros and all ones, are simple. Zero is zero no matter the number system, so a 4bit hex system always produces zero.
0h = 0000 binary = 0 decimal
The maximum value represented with four bits is 15 (base 10) or 1111 (base 2). Associate 0xF with 1111 (base 2) and 15 (base 10).
0xf = 1111 (binary) = 15 (decimal)
The First Ten
The first ten hex digits, 0 to 9, are the same as decimal. We already know that 0h is zero, so that leaves us with nine to learn. The binary equivalents of 1 to 9 are easily calculated mentally by converting from decimal to binary since we can easily add 1, 2, and 4 in our heads. The number 8 is needed twice to find the values for 8d and 9d.
0x0 0000_{2} 0_{10}
0x1 0001_{2} 1_{10} 0x2 0010_{2} 2_{10} 0x3 0011_{2} 3_{10} 0x4 0100_{2} 4_{10} 0x5 0101_{2} 5_{10} 0x6 0110_{2} 6_{10} 0x7 0111_{2} 7_{10} 0x8 1000_{2} 8_{10} 0x9 1001_{2} 9_{10}
0xF 1111_{2} 15_{10}
If 0x7 poses a problem, always remember that decimal 7 in binary is three bits, the maximum value expressed in a 3bit system.
The Letter Digits
These are the last six digits, A to F. 0xA is the first hex digit that begins with a letter, so memorize 1010 (binary) for 0xA and 10 (decimal), and use 1010 (binary) as an anchor point to divide the “number range” (0 to 9) from “letter range” (A to F).
So far, we have learned twelve of the sixteen hex digits: 0 to 9, A, and F. The remaining four — B, C, D, E — are the only patterns that require rote memorization. Again, use 0xA as an anchor point. If 1010 (binary) is 0xA, then add 1 to it to produce 1011 (binary), which is 0xB, and so on until 1111b is reached.
With practice, these sixteen values with their associated bit patterns and decimal values will become second nature.
Practice
Let’s practice!
Set 1
Convert each of the hex numbers into binary.

0x33

45h

100h

0101_{16}

0xabcdef
Set 1 Answers

00110011

01000101

000100000000

0000 0001 0000 0001 or 0001 0000 0001 (The leading zeros may be dropped, but not the inner zeros.)

1010 1011 1100 1101 1110 1111 (See the pattern?)
Set 2
Convert the following binary numbers to hexadecimal.

1010

110011

11111000011110001
Set 2 Answers
 0xa
 110011 –> 0011 0011 –> 33h (Split into groups of four bits from the right and pad with leading zeros before conversion for clarity.)
 1 1111 0000 1111 0001 (Divide into groups of four bits starting from the least significant bit) –> 0001 1111 0000 1111 0001 (Pad with leading zeros so all groups have four bits.)
0001 1111 0000 1111 0001 1 f 0 f 1 Result:
11111000011110001 = 0001 1111 0000 1111 0001 = 0x1f0f1
Set 3
Convert the following hex numbers into decimal.

3h

0xb

33_{16}

100 (base 16)

0x87

0xa11f (Calculator allowed for binary to decimal conversion)

0xffffffff (Calculator allowed for binary to decimal conversion)
Set 3 Answers

3 (decimal, same as hex number)

11 (decimal, look up the chart)

33h = 0011 0011 = 51 (decimal, this requires binary to decimal conversion instead of simple lookups)

100 (base 16) = 100000000 (base 2) = 256 (decimal)

0x87 = 1000 0111 = 135 (decimal)

0xa11f = 1010 0001 0001 1111 = 41,247 (decimal)

0xffffffff = 11111111 11111111 11111111 11111111 = 4,294,967,295 (decimal)
Take note of 6 and 7 above. These are large values expressed succinctly using hexadecimal. Often, hex numbers appear in even groups, such as 2, 4, 6, and 8. Number 6, groups of four hex digits, represents 16bit binary values, and number 7, groups of eight hex digits, represents 32bit binary values. Number 7 is a contrived problem to show the largest possible value that can be accessed using a 32bit processor with a 32bit address space. 32bit values are often shown in groups of eight hexadecimal digits.