30.9k views
3 votes
1. Write code to convert a decimal number to binary and vice versa Preliminaries • For this lab, create another folder, called lab13. • Navigate to the winpython folder on your computer. If you need help getting to the folder please refer back to lab 1. Once inside the winpython folder double click on "Spyder" icon and it will open up the Spyder IDE. I. Binary-Decimal Converter For this programming task you will write two functions. The first function named "decimalToBinary", which will take in a number in decimal format and return the number in binary form. The second function named binaryToDecimal", which will take in a binary number as a string and return the number in decimal form. 1. To get started create a new file and save it as Binary DecimalConverter.py. In the comment section on top, include your name, todays date and program name (Lab 13, Binary DecimalConverter.py). 2. Define a function and name it "decimal To Binary". This function will take in a decimal number and return it converted to binary form. There are many ways to implement this function. We will implement it without a stack and simply use a string object for the representation a binary number. Below is an example of how to convert a decimal number to a binary number. Refer back to the slides for more information, but basically we are diving by 2 and storing the remainder. 12520 22 2125 2162 1 231 15 7 1 1 0 NNNNNNN 1 1 3 1 0 1 12520 1111101 For the remainder of this write up, the parameter in the method definition will be referred to as "num". Inside the method create a variable named "bits" and assign an empty string to it. Write a while loop with the conditional "num >0". Inside the while loop you have to do two things: • Calculate the remainder of "num" divided by two. This can be accomplished with the modules operator. Cast the remainder to a string object and add it to the front of the variable name "bits". The reason why we add to the front and not the end, is because we read binary right to left. Update the variable name "num" by diving it by 2. When dividing by 2 we don't want decimal numbers so use the double division sign "r" also known as the floor division. After the while loop return the variable name "bits". 3. Write another function and name it "binaryToDecimal". This function should take in a parameter named "num" similar to the first function. Our implementation of this function will assume that the parameter "num" is coming in as a string object . For example: *1001101' or '100001101'. The function will traverse over the string from right to left and multiple the digits by 2". Wheren starts at 0 (right most) and goes up by one, as we go over the binary digits from right to left. Below is a screenshot of how this is done on paper. 1010112 => 1 1 x 20 1 x 21 0 x 22 1 x 23 = 0 x 2 = 1 x 25 = 00 ON 8 0 32 . 4310 Binary Base = 2 Column Column Column 2 128 2° 64 2 32 Columns Column 4 Columns Column 2 Column 1 2 2! 2 2 2 16 8 4 2 1 Weight In a program this can be accomplished using a for loop, and going through the string in reversed order. In python you can loop through a list in reversed order by simply saying "for i in reversed(num): Above the for loop you need to declare a variable which will keep the sum of all calculations and become the converted decimal number at the end. Also declare another variable to keep track of the "powers (n)". First assign 0 to it and increment it by 1 inside the for loop. After the for loop return the decimal number

User Teri
by
8.6k points

1 Answer

5 votes

To convert decimal numbers to binary, use the decimalToBinary function to divide the number by 2, collect remainders and reverse the order. To convert binary to decimal, use the binaryToDecimal function to multiply each binary digit by 2 raised to the power of its position and sum the results.

To convert a decimal number to binary and vice versa in Python, we can write two functions, decimalToBinary and binaryToDecimal.

For decimalToBinary function, we repeatedly divide the decimal number by 2 and store the remainder in a string in reversed order. Here's a step-by-step Python code that accomplishes that:

def decimalToBinary(num):
bits = ''
while num > 0:
bits = str(num % 2) + bits
num = num // 2
return bits

For the binaryToDecimal function, we iterate over the binary string in reversed order, multiply each digit by 2 raised to the power of its position, and sum it all up. The Python code for this looks like:

def binaryToDecimal(num):
decimal_number = 0
power = 0
for digit in reversed(num):
decimal_number += int(digit) * (2 ** power)
power += 1
return decimal_number

User Gtludwig
by
8.6k points