Binary to Decimal
Binary is a number system consisting of 2 numbers, 0 and 1. It works exactly like the most used number system, decimals. Binary to decimal conversion consists of multiplying the digit in the right column to the right power of 2. Something like this:
So, the decimal number of the given binary number would equal to:
which equals to 3757 base 10.So, what I did was take the binary number, change it into a string, put each individual digit into indexes in a list, reversed that list, then multiplied that digit by the index's power of 2.
def binary(number):
numb_array = []
for x in range(0, len(number)):
numb_array.append(number[x])
number = ""
for x in range(0, len(numb_array)):
number += numb_array[len(numb_array)-x-1]
output = 0
for x in range(0, len(number)):
output += int(number[x]) * (2 ** x)
return output
Ternary is the same thing, except that the 2 is replaced by 3:def ternary(number):
numb_array = []
for x in range(0, len(number)):
numb_array.append(number[x])
number = ""
for x in range(0, len(numb_array)):
number += numb_array[len(numb_array) - x - 1]
output = 0
for x in range(0, len(number)):
output += int(number[x]) * (3 ** x)
return output

Comments
Post a Comment