Hexadecimal

advertisement
Hexadecimal




Is base 16 – meaning there are 16 possible digits
Easily represents binary as each hex digit can represent 4 binary ones – a nibble.
Each column of the number represents a value that that digit is multiplied by.
Possible digits are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
Hexadecimal →
Denary↓
256
162
16
161
1
160
Calculation
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
33
58
93
174
203
259
868
1552
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
3
6
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
2
3
5
10
C
0
6
1
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
0
1
A
D
E
B
3
4
A
(256*0) + (16*0) + (1*0)
(256*0) + (16*0) + (1*1)
(256*0) + (16*0) + (1*2)
(256*0) + (16*0) + (1*3)
(256*0) + (16*0) + (1*4)
(256*0) + (16*0)+ (1*5)
(256*0) + (16*0) + (1*6)
(256*0) + (16*0) + (1*7)
(256*0) + (16*0) + (1*8)
(256*0) + (16*0) + (1*9)
(256*0) + (16*0) + (1*10)
(256*0) + (16*0) + (1*11)
(256*0) + (16*0) + (1*12)
(256*0) + (16*0) + (1*13)
(256*0) + (16*0) + (1*14)
(256*0) + (16*0) + (1*15)
(256*0) + (16*1) + (1*0)
(256*0) + (16*2) + (1*1)
(256*0) + (16*3) + (1*10)
(256*0) + (16*5) + (1*13)
(256*0) + (16*10) + (1*14)
(256*0) + (16*12) + (1*11)
(256*1) + (16*0) + (1*3)
(256*3) + (16*6) + (1*4)
(256*6) + (16*1)+ (1*10)
Hexadecimal to Denary Conversions
Examples of these are show in the table above.
1. Create a table with the factors of 16 as column headings (smallest factor on the right, largest
on the left)
2. Right to left, enter the digits of your hexadecimal number under the column headings
3. Multiply the column heading by the digit, as show in the table
4. Sum those totals to give your answer in denary
E.g. 16616 in denary:
256
16
1
1
6
6
Gives us (1*256) + (6*16) + (6*1) = 358
Converting between Hexadecimal and Binary
Converting either way between hex and are simple, as each hex digit corresponds directly to 4 binary
digits. Here we show the range of numbers that each nibble (4 bits) can cover)
Binary to Hexadecimal
1. Break the binary number into nibbles (4 bits) – 0100 0010 0111
2. Give each nibble the headings 1,2,4,8 (right to left) as shown
8
0
4
0
2
0
1
1
8
0
4
1
2
1
1
0
8
1
4
1
2
1
1
0
3. Each nibble translates to a single hex digit:
a. (0*8) + (0*4) + (0*2) + (1*1) = 1
b. (0*8) + (1*4) + (1*2) + (0*1) = 6
c. (1*8) + (1*4) + (1*2) + (0*1) = 13 = C
4. Gives us 16C
Hex to Binary
1. For each hex digit, create a nibble.
2. Using the 1,2,4,8 headings convert that number to binary
3. Gives us 0001 0110 0110
Hexadecimal
Denary
Binary
1
8
0
4
0
6
2
0
1
1
8
0
4
1
6
2
1
1
0
8
0
4
1
2
1
1
0
Denary to Hexadecimal Conversions
You can do conversions from Denary to Hex directly, but it is very long winded, particularly for large
numbers:
1. Start with the largest factor of 16 that is smaller than our target in denary
2. Is your denary target larger than or equal to the column multiplier? If not, set to 0
3. If so, work out how many times the target can be divided by the column multiplier. Call this
the divisor. Enter that number under the column, remembering to convert digits higher
than 9 to their Hex equivalent
4. Work out the remainder by subtracting (the last divisor * 16) from the target. That is our
new target.
5. Repeat 2-4 until you have no remainder.
E.g. Denary 358 to hex:
1.
2.
3.
4.
5.
6.
7.
8.
9.
1, 16 and 256 are smaller than 358, but 4096 is larger, so we’ll start with 256 as our MSB
358 is larger than 256
358 is divisible by 256 only once, so our divisor is 1 – we enter that in the 256 column
358 – (1*256) = 102, which is our new target
102 is larger than 16, our next column heading (Factor of 16 - 161)
102 is divisible by 16 6 times, so enter 6 in the 16 column
102 – (6*16) = 102-96 = 6, which is our new target
6 is larger than 1, and 6 is divisible by 1 6 times, so enter 6 in the final column
Gives us 1 6 6 in Hex
There is an easier way which involves converting Denary -> Binary -> Hex.
1. Convert the denary number into binary (Use one of the methods from the denary<->binary
info sheet, method 1 is shown here)
2. Split your binary number into 4 bit nibbles
3. Each nibble can be quickly converted into a hexadecimal number. This gives us our hex
digits.
E.g. denary 358 to binary:












2048
0
The first number smaller than 358 is 256. Put a 1 in that column and 0s in every one before
New target is 358-256 = 102
128 is bigger than 102 so we put a 0 there and keep our target
64 is smaller than 102 so we put a 1 in that column
New Target is 102-64 = 38
32 is less than 38 so a 1 in that column
New target is 38-32 = 6
16 is more than 6 so a 0 in that column, target remains 6
8 is more than 6 so 0 in that column, target remains 6
4 is less than 6 so 1 in that column
Target is 6-4 = 2
2 is the same as our target of 2 so we put a 1 in that column, and have no remaining target,
so any further columns (just the one here) must be 0
1024
0
512
0
256
1
128
0
64
1
32
1
16
0
8
0
4
1
2
1
This gives us 3 nibbles (if we used less columns we could pad them with 0s to make our nibbles).
Each nibble has simple 8, 4, 2, 1 headings so the hex value can be worked out quickly
1. 0 0 0 1 = 1
2. 0 1 1 0 = 4 + 2 = 6
3. 0 1 1 0 = 4 + 2 = 6
1
0
So this 358 in hex is 166. It seems a little long-winded to do it this way, but the individual
calculations are actually much easier, so this method is much more reliable and you’ll soon
remember some of the denary/binary/hex conversions without having to work them out.
Exercise:
1. Convert the following numbers to Hexadecimal, using the direct method
a. 13
b. 21
c. 95
d. 469
2. Convert the following numbers to Hexadecimal, going via binary
a. 29
b. 146
c. 3698
3. Convert the following hexadecimal numbers to Denary
a. 4F
b. D52
c. 123
Download