Algorithm to Convert From Another Base to Decimal
To convert hexadecimal, binary, octal, and any other numeric notation systems to decimal nueric notation system:
Suppose the number is XYZ, and the numeric notation system is N-based (ie, 16, 2, 8, etc.):
The 10-based decimal number = X*N2+ Y*N1 + Z
For example, 16-based FFE = 15*162 + 15*16 1 + 14 = 4,094
Another example, 2-based 1101 = 1*23 + 1*22 + 0*21 + 1 = 13
Algorithm to Convert From Decimal To Another Base
Let n be the decimal number.
Let m be the number, initially empty, that we are converting to. We'll be composing it right to left.
Let b be the base of the number we are converting to.
Repeat until n becomes 0
Divide n by b, letting the result be d and the remainder be r.
Write the remainder, r, as the leftmost digit of b.
Let d be the new value of n.
Let's use the algorithm to convert 45 into binary.
Let n = 45.
Let b = 2.
Repeat
45 divided by b is 45/2 = 22 remainder 1. So d=22 and r=1. So m= 1 and the new n is 22.
22 divided by b is 22/2 = 11 remainder 0. So d=11 and r=1. So m= 01 and the new n is 11.
11 divided by b is 11/2 = 5 remainder 1. So d=5 and r=1. So m= 101 and the new n is 5.
5 divided by b is 5/2 = 2 remainder 1. So d=2 and r=1. So m= 1101 and the new n is 2.
2 divided by b is 2/2 = 1 remainder 0. So d=1 and r=0. So m= 01101 and the new n is 1.
1 divided by b is 1/2 = 0 remainder 1. So d=0 and r=1. So m=101101 and the new n is 0. So 4510 = 1011012
Let's use it to convert 99 into binary.
Let n = 99.
Let b = 2.
Repeat
99 divided by b is 99/2 = 49 remainder 1. So d=49 and r=1. So m= 1 and the new n is 49.
49 divided by b is 49/2 = 24 remainder 1. So d=24 and r=1. So m= 11 and the new n is 24.
24 divided by b is 24/2 = 12 remainder 0. So d=12 and r=0. So m= 011 and the new n is 12.
12 divided by b is 12/2 = 6 remainder 0. So d=6 and r=0. So m= 0011 and the new n is 6.
6 divided by b is 6/2 = 3 remainder 0. So d=3 and r=0. So m= 00011 and the new n is 3.
3 divided by b is 3/2 = 1 remainder 1. So d=1 and r=1. So m= 100011 and the new n is 1.
1 divided by b is 1/2 = 0 remainder 1. So d=0 and r=1. So m=1100011 and the new n is 0. So 9910 = 11000112
Let's use it to convert 45 into hexadecimal.
Let n = 45.
Let b = 16.
Repeat
45 divided by b is 45/16 = 2 remainder 13. So d=2 and r=13. So m= D and the new n is 2.
2 divided by b is 2/16 = 0 remainder 2. So d=0 and r=2. So m=2D and the new n is 0. So 4510 = 2D16.
Let's use it to convert 99 into hexadecimal.
Let n = 99.
Let b = 16.
Repeat
99 divided by b is 99/16 = 6 remainder 3. So d=6 and r=3. So m= 3 and the new n is 6.
6 divided by b is 6/16 = 0 remainder 6. So d=0 and r=6. So m=63 and the new n is 0. So 9910 is 6316.