ME Labs, Inc.
Hexadecimal Number SystemA 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:
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:
In the Hexadecimal number system, the hex values greater than 9 carry the following decimal value:
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:
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:
Binary to Hex Conversion
It is easy to convert from an integer binary number to hex. This is accomplished by:
For example, the binary value 1010111110110010 will be written:
Hex to Binary Conversion
It is also easy to convert from an integer hex number to binary. This is accomplished by:
For example, the hex value $AFB2 will be written:
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.
As you can see, we are back with the original number. That is what we should expect.
ME Labs, Inc.
PO Box 8250
Asheville NC 28814
(719) 520-1867 fax