Books and References © Rance Necaise
Signed IntegersChapter 2. Data RepresentationCharacter Data

Hexadecimal Representation

When dealing with numerical values, humans prefer to work with the familiar decimal notation. But computers work with values in binary notation and sometimes we have to work with those as well. Specifying values in binary notation, especially when they consist of 32-bits or more

01011010011100110010110011100110

can be tedious and error prone. Instead of working directly with binary values, however, we typically specify values in a third positional numbering system. The hexadecimal number system is a base 16 system that allows for easy translation with binary values. As a positional system, each column has a value that is a power or 16:

and each column can have one of 16 different numerals as illustrated in Table 1. The first ten numerals are the same as those used with decimal values (0 - 9) and the remaining six numerals correspond to the letters A - F.

Decimal Hexadecimal
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 A
11 B
12 C
13 D
14 E
15 F
Table 1. The 16 digits in the hexadecimal number system.

In computer science, hexadecimal notation is typically used to specify binary values in a high-level language without having to write all of the bits. It is also widely used with assembly languages and machine code. Thus, in this section, we limit our focus to the number conversions between binary and hexadecimal values only.

Hexadecimal to Binary

Converting a hexadecimal value to its binary equivalent is a rather simple process. We simply convert each digit in the hexadecimal value to its equivalent 4-bit binary value, which are listed in Table 2 for easy reference.

Decimal Hexadecimal Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111
Table 2. The 16 hexadecimal digits and their corresponding binary values.

To illustrate the process, suppose we want to convert the value B716 to binary.

Each hexadecimal digit will be converted to a 4-bit binary value for a total of 8 bits.

Each 4-bit binary value will take the same relative position as the corresponding hexadecimal digit in the original hexadecimal value.

We start with the right-most digit and work our way to the left, one digit at a time. The 7, which has the same value in decimal, is equivalent to 0111 in binary using 4 bits.

The next digit is B, which has a decimal value of 11 and is equivalent to 1011 in binary.

The end result is a binary value of 101101112 in 8 bits.

NoteNote

In programming languages, hexadecimal values are specified by preceding the hex digits with 0x. For example, the value above would be expressed as

    0xB7

To assign this value to a variable (in Python), the notation would be

    x = 0xB7

Binary to Hexadecimal

The reverse conversion can be performed in a similar fashion. Suppose we want to convert the 16-bit value

01101101111100102

to its hexadecimal equivalent. Follow the steps illustrated below to see how the conversion is performed:

Question 1.

Convert each of the following numerical values to the indicated numbering system. When entering an answer in hexadecimal, enter hexadecimal letters in uppercase.

Question Answer Explanation

Convert 100110112 to base 16.

9B

Convert 110111102 to base 16.

DE

Convert 3C16 to binary using 8 bits.

00111100

Convert 8710 to hexadecimal using 8 bits.

57

The binary equivalent of 87 is 01010111 which corresponds to 57 in hexadecimal.

Question 2.

Convert the original 32-bit binary value example

01011010011100110010110011100110

to its equivalent hexadecimal value.

The 32-bit binary value will result in the 8-bit hexadecimal value

5A732CE6

which was derived by: 1) splitting the 32 bits into groups of 4 starting on the right:

0101 1010 0111 0011 0010 1100 1110 0110

2) converting each 4-bit group to its equivalent hexadecimal value

0101 1010 0111 0011 0010 1100 1110 0110
  5    A    7    3    2    C    E    6

3) pushing the hexadecimal digits together to form the 16-base value.

Signed IntegersChapter 2. Data RepresentationCharacter Data
Page last modified on September 12, 2021, at 11:45 AM