You are on page 1of 7

Hexadecimal Number System

A big problem with the binary system is verbosity. To represent the value 202 requires eight
binary digits.

The decimal version requires only three decimal digits and, thus, represents numbers much
more compactly than does the binary numbering system. This fact was not lost on the
engineers who designed binary computer systems.

When dealing with large values, binary numbers quickly become too unwieldy. The
hexadecimal (base 16) numbering system solves these problems. Hexadecimal numbers offer
the two features:

 hex numbers are very compact


 it is easy to convert from hex to binary and binary
to hex.

The Hexadecimal system is based on the binary system using a Nibble or 4-bit boundary. In
Assembly Language programming, most assemblers require the first digit of a hexadecimal
number to be 0, and place an "h" at the end of the number to denote the number base.  In
PICBASIC, we simply put a "$" at the left end of the number.

The Hexadecimal Number System:

 uses base 16
 includes only the digits 0 through 9 and the letters A, B, C, D, E, and F

In the Hexadecimal number system, the hex values greater than 9 carry the following decimal
value:

Binary Decimal Hex


%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 10 $A
%1011 11 $B
%1100 12 $C
%1101 13 $D
%1110 14 $E
%1111 15 $F

This table provides all the information you'll ever need to convert from one number base into
any other number base for the decimal values from 0 to 16.

To convert a hexadecimal number into a binary number, simply break the binary number into
4-bit groups beginning with the LSB and substitute the corresponding four bits in binary for
each hexadecimal digit in the number.

For example, to convert $ABCD into a binary value, simply convert each hexadecimal digit
according to the table above. The binary equivalent is:

$ABCD = 1010 1011 1100 1101

To convert a binary number into hexadecimal format is almost as easy. The first step is to pad
the binary number with leading zeros to make sure that the the binary number contains
multiples of four bits. For example, given the binary number 1011001010, the first step
would be to add two bits in the MSB position so that it contains 12 bits. The revised binary
value is 001011001010.

The next step is to separate the binary value into groups of four bits, e.g., 0010 1100 1010.
Finally, look up these binary values in the table above and substitute the appropriate
hexadecimal digits, e.g., %0010=$2, %1100=$C, %1010=$A.  %001011001010=$2CA.

The weighted values for each position is as follows:

163 162 161 160


4096 256 16 1

Binary to Hex Conversion

It is easy to convert from an integer binary number to hex. This is accomplished by:

1. Break the binary number into 4-bit sections from


the LSB to the MSB.
2. Convert the 4-bit binary number to its Hex
equivalent.
For example, the binary value 1010111110110010 will be written:

1010 1111 1011 0010


A F B 2

Hex to Binary Conversion

It is also easy to convert from an integer hex number to binary. This is accomplished by:

1. Convert the Hex number to its 4-bit binary


equivalent.
2. Combine the 4-bit sections by removing the spaces.

For example, the hex value $AFB2 will be written:

A F B 2
1010 1111 1011 0010

This yields the binary number 1010111110110010.

Hex to Decimal Conversion

To convert from Hex to Decimal, multiply the value in each position by its hex weight and
add each value. Using the value from the previous example, $AFB2, we would expect to
obtain the decimal value 44978.

(A*163) + (F*162) + ( B*161) + (2*160) =

(10*4096) + (15*256) + (11*16) + (2*1) =

40960 + 3840 + 176 + 2 = 44978

Decimal to Hex Conversion

To convert decimal to hex is slightly more difficult. The typical method to convert from
decimal to hex is repeated division by 16. While we may also use repeated subtraction by the
weighted position value, it is more difficult for large decimal numbers.

Repeated Division By 16

For this method, divide the decimal number by 16, and write the remainder on the side as the
least significant digit. This process is continued by dividing the quotient by 16 and writing
the remainder until the quotient is 0. When performing the division, the remainders which
will represent the hex equivalent of the decimal number are written beginning at the least
significant digit (right) and each new digit is written to the next more significant digit (the
left) of the previous digit. Consider the number 44978.

Division Quotient Remainder Hex Number


44978 / 16 2811 2 2
2811 / 16 175 11 B2
175 / 16 10 15 FB2
10 / 16 0 10 AFB2

As you can see, we are back with the original number. That is what we should expect.

3.4 The Hexadecimal Numbering System

A big problem with the binary system is verbosity. To represent the value 20210 requires eight
binary digits. The decimal version requires only three decimal digits and, thus, represents
numbers much more compactly than does the binary numbering system. This fact was not
lost on the engineers who designed binary computer systems. When dealing with large
values, binary numbers quickly become too unwieldy. Unfortunately, the computer thinks in
binary, so most of the time it is convenient to use the binary numbering system. Although we
can convert between decimal and binary, the conversion is not a trivial task. The hexadecimal
(base 16) numbering system solves these problems. Hexadecimal numbers offer the two
features we're looking for: they're very compact, and it's simple to convert them to binary and
vice versa. Because of this, most computer systems engineers use the hexadecimal numbering
system. Since the radix (base) of a hexadecimal number is 16, each hexadecimal digit to the
left of the hexadecimal point represents some value times a successive power of 16. For
example, the number 123416 is equal to:

1 * 163   +   2 * 162   +   3 * 161   +   4 * 160

or

4096 + 512 + 48 + 4 = 466010.

Each hexadecimal digit can represent one of sixteen values between 0 and 1510. Since there
are only ten decimal digits, we need to invent six additional digits to represent the values in
the range 1010 through 1510. Rather than create new symbols for these digits, we'll use the
letters A through F. The following are all examples of valid hexadecimal numbers:

123416   DEAD16   BEEF16   0AFB16   FEED16   DEAF16
Since we'll often need to enter hexadecimal numbers into the computer system, we'll need a
different mechanism for representing hexadecimal numbers. After all, on most computer
systems you cannot enter a subscript to denote the radix of the associated value. We'll adopt
the following conventions:

 All hexadecimal values begin with a "$" character, e.g., $123A4.


 All binary values begin with a percent sign ("%").
 Decimal numbers do not have a prefix character.
 If the radix is clear from the context, this text may drop the leading "$" or "%"
character.

Examples of valid hexadecimal numbers:

$1234  $DEAD  $BEEF  $AFB  $FEED  $DEAF

As you can see, hexadecimal numbers are compact and easy to read. In addition, you can
easily convert between hexadecimal and binary. Consider the following table:

Table 4: Binary/Hex Conversion

Binary Hexadecimal

%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

This table provides all the information you'll ever need to convert any hexadecimal number
into a binary number or vice versa.

To convert a hexadecimal number into a binary number, simply substitute the corresponding
four bits for each hexadecimal digit in the number. For example, to convert $ABCD into a
binary value, simply convert each hexadecimal digit according to the table above:

0 A B C D Hexadecimal

0000 1010 1011 1100 1101 Binary

To convert a binary number into hexadecimal format is almost as easy. The first step is to pad
the binary number with zeros to make sure that there is a multiple of four bits in the number.
For example, given the binary number 1011001010, the first step would be to add two bits to
the left of the number so that it contains 12 bits. The converted binary value is
001011001010. The next step is to separate the binary value into groups of four bits, e.g.,
0010_1100_1010. Finally, look up these binary values in the table above and substitute the
appropriate hexadecimal digits, i.e., $2CA. Contrast this with the difficulty of conversion
between decimal and binary or decimal and hexadecimal!

Since converting between hexadecimal and binary is an operation you will need to perform
over and over again, you should take a few minutes and memorize the table above. Even if
you have a calculator that will do the conversion for you, you'll find manual conversion to be
a lot faster and more convenient when converting between binary and hex.

 
The Hexadecimal Number System  
 
  Hexadecimal Number System (Base-16)
The hexadecimal number system uses SIXTEEN values to
represent numbers. The values are,
                                     0 1 2 3 4 5 6 7 8 9 A B C D E F
  with 0 having the least value and F having the greatest value. Columns are used in
the same way as in the decimal system, in that the left most column is used to
represent the greatest value.  As we have seen in the decimal system, the values in the
set (0 and 1) repeat, in both the vertical and horizontal directions.
                                0 - F, 10 -1F, 20 - 2F, 30 - 3F ......
        Hexadecimal is often used to represent values (numbers and memory addresses)
in computer systems.
 

Converting hexadecimal to decimal


Problem: Convert 176 in hexadecimal to decimal.
Each column represents a power of 16, Decimal Binary Hexadecimal
0 0000 0
164 becomes
4 * 160 = 4 1 0001 1
1
6 * 16 = 96 2 0010 2
2
1 * 16 = 256 3 0011 3
adding together gives 356 4 0100 4
5 0101 5
Converting binary to hexadecimal
Problem: Convert 110110 to hexadecimal. 6 0110 6
Each hexadecimal digit represents 4 binary bits. Split 7 0111 7
the binary number into groups of 4 bits, starting from 8 1000 8
the right.
9 1001 9
11   0110
=3   =6 10 1010 A
=36 in hexadecimal 11 1011 B
12 1100 C
Converting decimal to hexadecimal 13 1101 D
Problem: Convert 232 decimal to hexadecimal.
14 1110 E
Use the same method used earlier to divide decimal
to 15 1111 F
binary, but divide by 16.
232 / 16 = 14 with a remainder of 8
14 / 16 = 0 with a remainder of E (14 decimal = E)
then answer is E816
Notice the suffix added to indicate the number base

You might also like