Conversion of one number system to another

Conversion to Decimal

We can use following formula:

N(base r)=i=0n1diri N_{(base\ r)} = \sum_{i=0}^{n-1} d_i r^i

where,
N is the number with Radix r
n is the total number of digits in number N
di is the value of digit at position i

Let"s try with converting Decimal to Decimal :) :

Here,
N = 2024
r = 10
d0 = 4
d1 = 2
d2 = 0
d3 = 2

So,
2024(base 10)=4100+2101+0102+2103 2024_{(base\ 10)} = 4 * 10^0 + 2 * 10^1 + 0 * 10^2 + 2 * 10^3
=41+210+0100+21000 = 4 * 1 + 2 * 10 + 0 * 100 + 2 * 1000
=4+20+0+2000=2024(base 10) = 4 + 20 + 0 + 2000 = 2024_{(base\ 10)}

Trying with binary,

Here,
N = 1101
r = 2
d0 = 1
d1 = 0
d2 = 1
d3 = 1

So,
1101(base 2)=120+021+122+123 1101_{(base\ 2)} = 1 * 2^0 + 0 * 2^1 + 1 * 2^2 + 1 * 2^3
=11+02+14+18 = 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8
=1+0+4+8=13(base 10) = 1 + 0 + 4 + 8 = 13_{(base\ 10)}

For binary, another way to convert is to add the value of 2 to the power of the position where the value is 1.

1 1 0 1
d3 d2 d1 d0
23 22 - 20
8 4 - 1

adding all of them gives us 13. We ignored d1 because the value at that position is 0.

Let"s try with converting Hexadecimal to Decimal :

Here,
N = 6D1A
r = 16
d0 = A
d1 = 1
d2 = D
d3 = 6

So,
6D1A(base 16)=A160+1161+D162+6163 6D1A_{(base\ 16)} = A * 16^0 + 1 * 16^1 + D * 16^2 + 6 * 16^3
Converting A and D to their decimal equivalent:
=10160+1161+13162+6163 = 10 * 16^0 + 1 * 16^1 + 13 * 16^2 + 6 * 16^3
=101+116+13256+64096 = 10 * 1 + 1 * 16 + 13 * 256 + 6 * 4096
=10+16+3328+24576=27930(base 10) = 10 + 16 + 3328 + 24576 = 27930_{(base\ 10)}

Converting Decimal to other number system

  • Take the decimal number D
  • Divide by radix R
  • Note down the remainder, r
  • Divide the quotient q, with radix R again and again note down the remainder
  • Repeat this until q<Rq<R
  • Write down the remainders bottom-up from least significant to most significant digit

Example, converting 19153 to Hexadecimal:

Number Division Quotient Remainder
19153 19153/16=119719153/16 = 1197, r=1 1197 1
1197 1197/16=741197/16 = 74, r=13 74 13
74 74/16=474/16 = 4, r=10 4 10
4 4/16=04/16 = 0, r=4 0 4

Stopped as r < R, remainder is less than radix

Writing the remainders from bottom to top, from last to first:

4 : 10 : 13 : 1 = 4AD1base 16

Conversion from Hexadecimal to Binary

Simply convert the individual digits to their binary equivalent, also add extra 0s so that the binary equivalent number becomes 4 digit one.

4 A D 1
0100 1010 1101 0001

and combining them, we get
(4AD1)16 = (0100101011010001)2

We can remove the first zero, so it becomes (100101011010001)2

If we want to convert binary to hexadecimal, do the reverse, but first break the binary number to group of 4 digits starting from the least significant bit, add the number of 0s in the group where the number of digits in less than 4 to complete it, and then write the binary equivalent to it.

Taking the above example, to convert (100101011010001)2 to hexadecimal:

0100 1010 1101 0001
4 A D 1

and combining them, we get
(0100101011010001)2 = (4AD1)16

Here, we broke the bits into groups of 4 from the least significant bit(right to left), and added a 0 to make 100 4-digit one, 0100, and then converted those groups to hexadecimal.


© progshala.in