Data Types  «Prev  Next»
Lesson 5 Hexadecimal numbers
Objective Convert between binary and hexadecimal numbers.

Convert between Binary and Hexadecimal numbers (Operation)

Converting numbers between binary and hexadecimal is a straightforward process due to the simple relationship between the two bases: each hexadecimal digit corresponds exactly to a group of four binary digits (bits), known as a nibble. Here is a step-by-step process for both conversions:

Converting Binary to Hexadecimal:

  1. Group the Binary Number: Divide the binary number into groups of four bits starting from the right. Add leading zeros if necessary to make the last group to the left have four bits.
  2. Convert Each Group: Convert each group of four binary digits to their hexadecimal equivalent using the following mapping:
       0000 = 0
       0001 = 1
       0010 = 2
       0011 = 3
       0100 = 4
       0101 = 5
       0110 = 6
       0111 = 7
       1000 = 8
       1001 = 9
       1010 = A
       1011 = B
       1100 = C
       1101 = D
       1110 = E
       1111 = F
    
  3. Concatenate Results: Write down the hexadecimal digits in the same order as the binary groups to get the final hexadecimal number.
Example: Binary: 110101101001
Groups: 0110 1011 0100 1
Hexadecimal: 6B4 (add a leading zero to the last group to make it '0001' => 1)
So, the binary 110101101001 is 6B41 in hexadecimal.

Converting Hexadecimal to Binary:

  1. Separate Hexadecimal Digits: Write down each hexadecimal digit separately.
  2. Convert Each Digit: Convert each hexadecimal digit to its four-digit binary equivalent using the mapping provided above.
  3. Concatenate Binary Groups: Combine the binary groups in the same order as the hexadecimal digits to form the final binary number.

Example: Convert Hexadecimal 1A3 to Binary:
1 -> 0001  
A -> 1010  
3 -> 0011  

So, the hexadecimal 1A3 is 000110100011 in binary.
Remember that the leading zeros in the binary representation can be dropped if they are not necessary for the value's significance or bit alignment in a specific context.
One shortcoming in expressing numbers in binary form is that large numbers require a lot of digits. For example, the binary equivalent of the decimal number 123,456,789 is 111010110111100110100010101. To address this problem it is quite common to express binary numbers as hexadecimal numbers. The hexadecimal number system[1] is a base 16 number system. Each digit in a hexadecimal number can be one of the numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9, or one of the letters A, B, C, D, E, or F. Because the base of the hexadecimal number system is equal to 24 (since 16 is 2 to the 4th power), each digit in a hexadecimal number corresponds to four digits in the equivalent binary number. Here are the first 16 hexadecimal numbers along with their binary and decimal equivalents.

Hexadecimal Binary Decimal
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15

Convert from Binary to Hexadecimal

To convert from binary to hexadecimal simply replace each group of four binary digits with the corresponding hexadecimal digit. For example, to convert the binary number 111010110111100110100010101 from the example above to hexadecimal, we write the digits in groups of four (from right to left), and then replace each group with the corresponding hexadecimal digit, like this:

0111 0101 1011 1100 1101 0001 0101
7    9    C    B    C    1  5
Thus the binary number 111010110111100110100010101 is equivalent to the hexadecimal number 79CBC15.
If you like, you can convert 79CBC15 directly to decimal like this:
 
7 * 166 + 9 * 165 + 12 * 164 + 11 * 163 + 
12 * 162 + 1 * 161 + 5 * 160

to verify that this is indeed the decimal number 123,456,789.
Converting from hexadecimal to binary is also quite easy. Simply replace each hexadecimal digit with the four corresponding binary digits. For example, consider the hexadecimal number 3F2A. Replacing each hexadecimal digit with the corresponding four binary digits we have:
3    F    2    A
0011 1111 0010 1010

Thus the hexadecimal number 3F2A is equivalent to the 16-bit binary number 001111100101010.
We have yet to consider how negative integers are stored. We'll explore this topic in the next lesson.

Hexadecimal Numbers - Quiz

Click the Quiz link below to test your understanding of hexadecimal numbers.
Hexadecimal Numbers - Quiz

[1]Hexadecimal number system: Base 16 number system.