zaro

How to Convert Between Binary, Decimal, Hexadecimal, and Octal?

Published in Number Systems 3 mins read

Converting between binary, decimal, hexadecimal, and octal number systems involves understanding their bases and using specific methods for each conversion. Here's a comprehensive guide:

Understanding Number Bases

  • Binary (Base-2): Uses only 0 and 1.
  • Decimal (Base-10): The standard number system we use daily (0-9).
  • Hexadecimal (Base-16): Uses 0-9 and A-F (A=10, B=11, C=12, D=13, E=14, F=15).
  • Octal (Base-8): Uses 0-7.

Conversion Methods

1. Binary to Decimal:

  • Multiply each bit by 2 raised to the power of its position (starting from 0 on the rightmost bit).

  • Sum the results.

    Example: Binary 101101 becomes:

    (1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (1 * 2^2) + (0 * 2^1) + (1 * 2^0) = 32 + 0 + 8 + 4 + 0 + 1 = 45 (Decimal)

2. Decimal to Binary:

  • Repeatedly divide the decimal number by 2.

  • Note the remainders (0 or 1) at each step.

  • Write the remainders in reverse order to get the binary equivalent.

    Example: Decimal 45 becomes:

    • 45 / 2 = 22, Remainder = 1
    • 22 / 2 = 11, Remainder = 0
    • 11 / 2 = 5, Remainder = 1
    • 5 / 2 = 2, Remainder = 1
    • 2 / 2 = 1, Remainder = 0
    • 1 / 2 = 0, Remainder = 1

    Result: 101101 (Binary)

3. Binary to Hexadecimal:

  • Group the binary digits into sets of 4, starting from the right. If necessary, add leading zeros to complete the leftmost group.

  • Convert each group of 4 binary digits into its corresponding hexadecimal digit.

    Example: Binary 11010110 becomes:

    • 1101 0110
    • D 6

    Result: D6 (Hexadecimal)

    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

4. Hexadecimal to Binary:

  • Convert each hexadecimal digit into its corresponding 4-bit binary representation.

    Example: Hexadecimal D6 becomes:

    • D 6
    • 1101 0110

    Result: 11010110 (Binary)

5. Binary to Octal:

  • Group the binary digits into sets of 3, starting from the right. Add leading zeros if necessary.

  • Convert each group of 3 binary digits into its corresponding octal digit.

    Example: Binary 1011010 becomes:

    • 010 110 10 (Add a leading zero)
    • 2 6 2

    Result: 262 (Octal)

    Binary Octal
    000 0
    001 1
    010 2
    011 3
    100 4
    101 5
    110 6
    111 7

6. Octal to Binary:

  • Convert each octal digit into its corresponding 3-bit binary representation.

    Example: Octal 262 becomes:

    • 2 6 2
    • 010 110 010

    Result: 010110010 (Binary) Leading zeros can be dropped, resulting in 10110010.

7. Decimal to Hexadecimal:

  • Repeatedly divide the decimal number by 16.

  • Note the remainders at each step (0-15). If the remainder is 10-15, represent it as A-F.

  • Write the remainders in reverse order.

    Example: Decimal 214 becomes:

    • 214 / 16 = 13, Remainder = 6
    • 13 / 16 = 0, Remainder = 13 (D)

    Result: D6 (Hexadecimal)

8. Hexadecimal to Decimal:

  • Multiply each hexadecimal digit by 16 raised to the power of its position (starting from 0 on the rightmost digit). Remember that A=10, B=11, C=12, D=13, E=14, F=15.

  • Sum the results.

    Example: Hexadecimal D6 becomes:

    • (13 16^1) + (6 16^0) = (13 16) + (6 1) = 208 + 6 = 214 (Decimal)

9. Decimal to Octal:

  • Repeatedly divide the decimal number by 8.

  • Note the remainders at each step (0-7).

  • Write the remainders in reverse order.

    Example: Decimal 170 becomes:

    • 170 / 8 = 21, Remainder = 2
    • 21 / 8 = 2, Remainder = 5
    • 2 / 8 = 0, Remainder = 2

    Result: 252 (Octal)

10. Octal to Decimal:

  • Multiply each octal digit by 8 raised to the power of its position (starting from 0 on the rightmost digit).

  • Sum the results.

    Example: Octal 252 becomes:

    • (2 8^2) + (5 8^1) + (2 8^0) = (2 64) + (5 8) + (2 1) = 128 + 40 + 2 = 170 (Decimal)

By following these methods, you can accurately convert between binary, decimal, hexadecimal, and octal number systems.