Computer Science · Ch 2 — Encoding Schemes and Number System
Conversion from Binary Number to Octal/Hexadecimal Number and Vice-Versa
Conversion from Binary Number to Octal/Hexadecimal Number and Vice-Versa
Converting between binary and octal/hexadecimal never needs division or positional sums — it is pure grouping and substitution. A binary number is converted to an octal or hexadecimal number by making groups of 3 bits (octal) or 4 bits (hexadecimal) and replacing each group with its equivalent digit; the reverse direction expands each digit back into its bit group.
(A) Binary number to octal number
Group the bits 3 at a time, from right to left, and replace each 3-bit group with the corresponding octal digit (Table 2.5). If the number of bits is not a multiple of 3, add the required number of 0s at the most significant (left) end.
Example 2.9 — Convert (10101100)2 to octal.
Binary : 10101100 (8 bits — pad to 9 with a leading 0)
3-bit groups : 010 | 101 | 100 (grouped right to left)
Octal digits : 2 | 5 | 4
(10101100)2 = (254)8
(B) Octal number to binary number
Each octal digit is an encoding of a 3-digit binary number, so simply replace every octal digit by its 3-bit binary group.
Example 2.10 — Convert (705)8 to binary.
Octal digits : 7 | 0 | 5
3-bit groups : 111 | 000 | 101
(705)8 = (111000101)2
Why are 4 bits grouped together to get a hexadecimal number?
The base of hexadecimal is 16, and 16 = 2^4 — so four binary digits are exactly sufficient to represent all 16 hexadecimal symbols.
(C) Binary number to hexadecimal number
Group the bits 4 at a time, from right to left, and substitute each 4-bit group with its hexadecimal symbol (Table 2.6). If needed, add 0 bits at the most significant position to make the bit count a multiple of 4.
Example 2.11 — Convert (0110101100)2 to hexadecimal.
Binary : 0110101100 (10 bits — pad to 12)
4-bit groups : 0001 | 1010 | 1100 (grouped right to left)
Hex symbols : 1 | A | C
(0110101100)2 = (1AC)16
(D) Hexadecimal number to binary number
Each hexadecimal symbol encodes a 4-digit binary number, so substitute the 4-bit binary equivalent of each hexadecimal digit and join the groups together.
Example 2.12 — Convert (23D)16 to binary.
Hex digits : 2 | 3 | D
4-bit groups : 0010 | 0011 | 1101
(23D)16 = (001000111101)2
``` …