========================================
Converting to/from hexadecimal (base 16)
========================================
-Ian! D. Allen - idallen@idallen.ca - www.idallen.com
The following link is a very good explanation and procedure for converting
hexadecimal to decimal, including handling both unsigned numbers and
two's complement negative numbers using bit flipping and adding one:
http://www.madsci.org/posts/archives/2000-02/950277263.Cs.r.html
Converting a negative two's complement number written in binary, octal,
or hexadecimal, to decimal usually means flipping all the bits (doing a
one's complement) and then adding one, converting the result to a positive
number, and then putting a minus sign in front of the positive result.
Since it's tedious to convert hex to binary, flip the bits, add one,
then multiply it out to get decimal, here is a nice table that lets you
"flip" (do a one's complement) on any hex digit directly:
One's Complement (Bit-Flip) Table for Hexadecimal
-------------------------------------------------
0 <--> F ( 0000 <--> 1111 )
1 <--> E ( 0001 <--> 1110 )
2 <--> D ( 0010 <--> 1101 )
3 <--> C ( 0011 <--> 1100 )
4 <--> B ( 0100 <--> 1011 )
5 <--> A ( 0101 <--> 1010 )
6 <--> 9 ( 0110 <--> 1001 )
7 <--> 8 ( 0111 <--> 1000 )
(You can create the above bit-flip table quickly by simply "folding"
a list of 16 hex digits in half - write the hex digits down one side
and then up the other side. The sum of any row is "F" [15].)
Find the hex digit you are complementing (bit flipping) in the table,
and replace it by the other number on the same line: 4A23 becomes B5DC.
Once you've flipped the bits, you can complete the conversion process
by adding one to the bit-flipped result and converting to decimal.
EXAMPLE 1: Convert two's complement 16-bit hex A2C9h to decimal.
----------
Step 1: Check the sign. If negative, make it positive and save the sign.
A2C9 is negative (the sign bit is on, since "A" = 1010 binary)
Use the table on A2C9 to flip the bits in each hex digit,
giving 5D36 which is the one's complement,
and then add one +1
giving 5D37 (a positive number, since "5" is 0101 binary)
Step 2: multiply out the powers of 16 for each hex digit
5 times 16**3 = 5 times 4096 = 20480
D times 16**2 = 13 times 256 = 3328
3 times 16**1 = 3 times 16 = 48
7 times 16**0 = 7 times 1 = 7
Add them all up and get 23863
Step 3: put back the sign (saved from Step 1)
Answer -23863 (negative)
EXAMPLE 2: Convert unsigned 16-bit hex A2C9 to decimal.
----------
Step 1: Unsigned numbers are all positive. No need to check any sign.
Step 2: multiply out the powers of 16 for each hex digit
A times 16**3 = 10 times 4096 = 40960
2 times 16**2 = 2 times 256 = 512
C times 16**1 = 12 times 16 = 192
9 times 16**0 = 9 times 1 = 9
Add them all up and get TOTAL 41673
Step 3: put back the sign (saved from Step 1)
Unsigned numbers are always positive.
Answer 41673
EXAMPLE 3: Convert two's complement 20-bit hex A2C9 to decimal.
----------
Step 1: Check the sign. If negative, make it positive and save the sign.
A2C9h is actually 0A2C9h in hex, since we are told it is 20-bits wide.
Because the sign bit is off (since "0" = 0000 binary), this is a
positive number. No need to flip any bits.
Step 2: multiply out the powers of 16 for each hex digit
0 times 16**4 = 0 times 65536 = 0
A times 16**3 = 10 times 4096 = 40960
2 times 16**2 = 2 times 256 = 512
C times 16**1 = 12 times 16 = 192
9 times 16**0 = 9 times 1 = 9
Add them all up and get TOTAL 41673
Step 3: put back the sign (saved from Step 1)
This is a positive number.
Answer 41673
EXAMPLE 4: Convert two's complement 16-bit hex 1A8C to decimal.
----------
Step 1: Number 1A8Ch is positive, since the leftmost hex digit "1" is
0001 binary and we can see that the left (sign) bit is zero.
Step 2: multiply out the powers of 16 for each hex digit
1 times 16**3 = 1 times 4096 = 4096
A times 16**2 = 10 times 256 = 2560
8 times 16**1 = 8 times 16 = 128
C times 16**0 = 13 times 1 = 13
Add them all up and get TOTAL 6796
Step 3: put back the sign (saved from Step 1)
This is a positive number.
Answer 6796
EXAMPLE 5: Convert two's complement 16-bit hex A123h to decimal.
----------
Step 1: Check the sign. If negative, make it positive and save the sign.
A123h is negative (the sign bit is on, since "A" = 1010 binary)
Use the table on A123 to flip the bits in each hex digit,
giving 5EDC which is the one's complement,
and then add one +1
giving 5EDD (a positive number, since "5" is 0101 binary)
Step 2: multiply out the powers of 16 for each hex digit
5 times 16**3 = 5 times 4096 = 20480
E times 16**2 = 14 times 256 = 3584
D times 16**1 = 13 times 16 = 208
D times 16**0 = 13 times 1 = 13
Add them all up and get 24285
Step 3: put back the sign (saved from Step 1)
Answer -24285 (negative)
EXAMPLE 6: Convert unsigned 16-bit hex A123 to decimal.
----------
Step 1: Unsigned numbers are all positive.
Step 2: multiply out the powers of 16 for each hex digit
A times 16**3 = 10 times 4096 = 40960
1 times 16**2 = 1 times 256 = 256
2 times 16**1 = 2 times 16 = 32
3 times 16**0 = 3 times 1 = 3
Add them all up and get 41251
Step 3: put back the sign (saved from Step 1)
Answer 41251
See Also
--------
Any Base Converter: Convert numbers in any base up to 32:
http://www.cut-the-knot.org/binary.shtml
--
| Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada
| Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/
| College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/
| Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/