Number System Conversion

In this tutorial, you will learn how to convert numbers from one base system to another. We will see all possible sets of conversions among the four most used number systems. For example, Binary to Decimal, Hexadecimal to Octal, Decimal to Hexadecimal, etc.

Contents:

  1. Decimal to Binary, Octal, and Hexadecimal Conversion
  2. Binary, Octal, and Hexadecimal to Decimal Conversion
  3. Binary to Octal Conversion
  4. Octal to Binary Conversion
  5. Binary to Hexadecimal Conversion
  6. Hexadecimal to Binary Conversion
  7. Octal to Hexadecimal Conversion
  8. Hexadecimal to Octal Conversion

Decimal to Binary, Octal, and Hexadecimal Conversion

The process for conversion from Decimal to any other base follows these basic steps.

  • For a given number in decimal, we divide the integer part of the number by radix value, which we have to convert into and go on dividing till we get zero as quotient.
  • We keep a track of all the remainders at all stages and then write them in the reverse order that is from bottom-up, which gives us the integer part of the number in the required base.
  • Due to writing in reverse order, the first remainder becomes the least significant bit (LSB) and the last remainder becomes the most significant bit (MSB).
  • To obtain the fractional part, we multiply the entire fractional part with the radix and write down the integer part obtained from it.
  • We can repeat this multiplication step with the newly obtained fractional part from the previous multiplication, till we get the fractional part as zero and get the perfect fractional equivalent.
  • We write down the digits in the order we performed the multiplications. Usually, getting a number with its fractional part as zero takes long and we stop after 3-4 digits of precision.

The following figure shows Decimal to Octal, Hexadecimal, and Binary Conversion with the same number as an example.

Decimal to Octal, Hexadecimal, and Binary Conversion

As can be seen from the figure,

  • While converting to octal, hexadecimal, and binary, we divided the number by 8, 16, and 2 respectively, and wrote down the remainders in reverse order as our integer part.
  • For the fractional part, we have multiplied with the respective bases and have written them down in the order of our performed multiplication.

advertisement
advertisement

Binary, Octal, and Hexadecimal to Decimal Conversion

To convert any number from any other base to decimal, we simply express the number as its product of digits and corresponding radix-powers of its current base and add the obtained values. For example,

  • Octal to decimal: – We multiply the digits with powers of 8 according to their positions.
    (1456)8 = 1 x 83 + 4 x 82 + 5 x 81 + 6 x 80
    = 512 + 256 + 40 + 6
    = (814)10
  • Binary to decimal: – We multiply the digits with powers of 2 according to their positions.
    (100110.101)2 = 1 x 25 + 0 x 24 + 0 x 23 + 1 x 22 + 1 x 21 + 0 x 20 + 1 x 2-1 + 0 x 2-2 + 1 x 2-3
    = 32 + 0 + 0 + 4 + 2 + 0 + 0.5 + 0 + 0.125
    = (38.625)10
  • Hexadecimal to decimal: – We multiply the digits with powers of 16 according to their positions.
    (6D4.B0)16 = 6 x 162 + 13 x 161 + 4 x 160 + 11 x 16-1 + 0 x 16-2
    = 1536 + 48 + 4 + 0.6875 + 0
    = (1748.6875)10

Binary to Octal Conversion

For converting a binary number to octal we make use of the fact that all octal digits can be perfectly expressed with 3 binary bits. The steps that we follow are: –

  • For the integer part: We make groups of 3 bits beginning from the last bit, moving towards higher bits.
  • For fractional part: We begin making groups of 3 bits just after the radix point and move towards the right.
  • We then write down the corresponding octal value for each group of those three bits.
    For example,

    1. (110111001)2 can be written in octal as: – 1 1 0 | 1 1 1 | 0 0 1 = (671)8
    2. (011110.010101)2 can be written in octal as: – 0 1 1 | 1 1 0 |.| 0 1 0 | 1 0 1 = (36.25)8
  • In case, any group is left with lesser number of bits, more zeroes can be added in front of it (for the integer part) and at the end (for the fractional part), to make that group of 3 bits.
    For example,
    1. (11100111.11)2 can be written in octal as: – 1 1|1 0 0 | 1 1 1 |.| 1 1 when regrouped becomes 0 1 1 | 1 0 0 | 1 1 1 |.| 1 1 0 = (347.6)8

Octal to Binary Conversion

To convert from octal to binary system, we write down the corresponding values of each digit of octal in its binary 3-bits form. This is a reverse operation of what we did in the previous section. The following figure shows how to convert a number from octal to binary.

Octal to Binary Conversion

As is clear from the figure,

  • Each octal digit is converted into its 3-bit binary equivalent and obtained values are written all together to give the binary equivalent of the octal number.

Here is a table to always refer to while converting from octal to binary or vice-versa.

Positional Values(22 21 20) 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1
Binary 000 001 010 011 100 101 110 111
Octal 0 1 2 3 4 5 6 7

As shown in the table,

advertisement
  • The positional values given by 4 2 1, can be used to calculate binary equivalents of octal numbers by writing 1 for that positional value which contributes to the sum of that digit. For example, 7=4+2+1, so we can write it as 111, etc.

Binary to Hexadecimal Conversion

For converting a number from binary to hexadecimal, we make groups as we did for octal, but instead of 3-bits, we make 4-bit groups. Here are the steps to convert:

  • We make groups of 4-bit, from the right-most bit (LSB) to the left-most bit (MSB) for the integer part, and write down the corresponding hexadecimal values for each group.
  • For the fractional part, we begin grouping after the radix point and move towards the right.
  • Also, we can add zeroes in front of a group (for the integer part) and zeroes to the end of a group (for the fractional part), to make it a 4-bit group.

For example: –

  1. (1011110011.11101)2 can be written in hexadecimal as: –
    0010|1111|0011|.|1110|1000 = (2F3.E8)16
  2. (11010011.1010)2 can be written in hexadecimal as: –
    1101|0011|.|1010 = (D3. A)16

Hexadecimal to Binary Conversion

For hexadecimal to binary conversion, we write down each hexadecimal digit’s 4-bit binary equivalent, and group the results together to give the final converted value. Here is a figure that shows hexadecimal to binary conversion.

advertisement
Hexadecimal to Binary Conversion

As can be seen from the figure,

  • Each digit was converted individually from hexadecimal to binary and written together to give the binary form of the hexadecimal number.

Here is a table to refer to while converting a number from hexadecimal to binary and vice-versa.

Positional Values(23 22 21 20) 8 4 2 1 8 4 2 1 8 4 2 1 8 4 2 1 8 4 2 1 8 4 2 1 8 4 2 1 8 4 2 1
Binary 0000 0001 0010 0011 0100 0101 0110 0111
Hexadecimal 0 1 2 3 4 5 6 7
Binary 1000 1001 1010 1011 1100 1101 1110 1111
Hexadecimal 8 9 A B C D E F

As shown in the table,

  • The binary equivalent of hexadecimal digits can be remembered easily by adding the positional values of 8 4 2 1 by using 1 for only those positions, which add up to the sum of that digit. For example, 9=8+0+0+1, hence we write it as 1001.

Octal to Hexadecimal Conversion

Octal numbers cannot be directly converted into hexadecimal form. So we convert it to an intermediate decimal or binary form. Using decimal as an intermediate form, our calculations increase, so we use binary for quick conversion. The steps are shown below using an example:

To convert (672.34)8 into hexadecimal:

1st step: Convert the number into binary by converting each octal digit individually into its 3-bit binary code: – (110 111 010 . 011 100)

2nd step: Group by 4-bits from the right for the integer part and from the left for the fractional part: – (0001) (1011) (1010) . (0111) (0000)

3rd step: Write the corresponding hexadecimal digits to each 4-bit group: – (1BA.70)16

Hexadecimal to Octal Conversion

Hexadecimal numbers also need an intermediate form to convert into octal numbers. Here are the steps to follow:

To convert (A2B6.83)16 into octal:

1st step: Convert the hexadecimal number into binary form by expanding each digit individually into its 4-bit binary code: – (1010 0010 1011 0110 . 1000 0011)

2nd step: From the right group the digits into 3-bits for the integer part and start after the radix point for the fractional part: – (001) (010) (001) (010) (110) (110) . (100) (000) (110)

3rd step: Convert each 3-bit group into its equivalent octal digit: – (121266.406)8

Key Points to Remember

Here are some key points listed, which are to be remembered in “Number System Conversion”.

  • A decimal number can be converted to any base by dividing the integer part by that base repeatedly till the remainder is zero and writing the remainders in reverse order.
  • From any base, a number can be converted into decimal form by expanding the number as its digits and radix-power product, followed by adding up the values.
  • Binary can be converted into Octal and Hexadecimal by grouping into 3 and 4 bits respectively and writing the corresponding digits.
  • Octal or hexadecimal is converted back into binary by expanding each digit’s 3-bit or 4-bit binary codes respectively.
  • Octal and Hexadecimal numbers cannot be interconverted directly; they need to be converted first either into decimal or binary.
  • For grouping any number, we move from LSB to MSB for the integer part, and for the fractional part, we start grouping after the radix point till the last fractional digit.
  • On converting a number from a lower base to a higher one, the number of digits decreases for the integer part and increases for the fractional part.
  • On converting from a higher base to a lower one, the number of digits increases for the integer part and decreases for the fractional part.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.