Use Python 3.xx to code a Binary Conventor def binaryToDecimal(binary): x = len(
ID: 3691722 • Letter: U
Question
Use Python 3.xx to code a Binary Conventor
def binaryToDecimal(binary):
x = len(binary) - 1
sum = 0
for bit in binary:
sum += (int(bit)*(2**x))
x -= 1
return sum
def main():
if __name__ == '__main__':
main()
Use the binary converter code provided above, use these examples for the code you write:
01000111
01001100
01100001
01000100
01001111
You are going to decode this message by going from binary to decimal and then from decimal to ASCII (ordinal to character).
For example, print(ch(97)) results in ‘a.’
Do not write any code inside the function I've provided you, just use it.
Your code will consist of two functions:
1. binaryConverter(binaryCode) a. returns the decimal number, converted from binary.
2. main()
Explanation / Answer
define binaryconverter(binaryCode):
print "This is the functionfor Binary to Decimal Conversion"
// binaryCode is the binary number to be converted to decimal
decimal = 0
factor = 1;
while(binaryCode > 0):
if( (binaryCode % 10) == 1):
decimal += factor
binaryCode /= 10
factor = factor * 2
print "The Final Decimal Number is: ", decimal
def main():
if __name__ == '__main__':
print(binaryConverter(01000111))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.