Question

Write a program that reads a binary string (string of 0’s and 1’s) converting the binary...

Write a program that reads a binary string (string of 0’s and 1’s) converting the binary value into decimal. Allow the user to type in as many numbers as they want (one at a time) and end the program when they type in “0”. Use Horner’s method (given in step 3, below) to convert from binary to decimal.

Sample Run

Binary to Decimal Conversion
Enter a binary string: 1101
1101 = 13 decimal
Enter a binary string: 10011001
10011001 = 153 decimal

2

Enter a binary string: 1001001
1001001 = 73 decimal

Do the following

  1. Within the loop, input a binary string (note there is no checking that the input is only 0’s and 1’s). Use input() NOT eval(input()). Return the string in binStr.

  2. Initialize decimalValue to 0. This will hold the decimal value.

  3. Using a for loop whose limit is the length of the binary string (len(binStr)), process each digit of the binary string as follows (Horner’s method): //

            decimalValue = 0
            for k in range(len(binStr)):
    

    The next binary digit is binStr[k]. Call this digit. Multiply decimalValue by 2

    Using the ord() function convert each binary digit (‘0’ or ‘1’) to its integer value by subtracting 48 from the ord() value (i.e. ord(digit)-48). Alternately you can use the int() function on each binary digit which does the same thing. Add the integer value of the digit to decimalValue.

  4. Output the binary string and its decimal value as shown above.

Homework Answers

Answer #1

##TELL ME WHAT IS WRONG WITH THE CODE

##IF YOU ARE SATISFIED WITH THE CODE, KINDLY LEAVE A LIKE, ELSE COMMENT TO CLEAR DOUBTS

CODE:

while True:
binStr = input("Enter a binary string: ")
if binStr == "0":
break
decimalValue = 0
for k in range(len(binStr)):
nextBinaryDigit = ord(binStr[k]) - 48
decimalValue *= 2
decimalValue += nextBinaryDigit
print(binStr,"=",decimalValue,"decimal\n")

OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In C++ Write a program that will convert a string of binary digits to their decimal...
In C++ Write a program that will convert a string of binary digits to their decimal equivalent. For convenience limit the binary number to 16 bits. Write the decimal equivalent to the screen. For this program you must read in the binary number as a string type. If you were to enter a large binary number like 110110001100111 as a decimal value it would be too large to fit in an int type.
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
C++ Write a function that returns a string of 1's and 0's. This needs to be...
C++ Write a function that returns a string of 1's and 0's. This needs to be the binary representation of a number. Do not use loops. Use only recursion. Do not change the function's parameters or add more parameters. Do not change the main program. #include #include string bin(int number) { //Code here } int main() {    cout << bin(43) << endl; // Should be 101011    return 0; }
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal....
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal. 1. Request input data type. 2. Request input data. 3. Request output data type. 4. Output the data. The suggested approach was to take the input data as a string. But I am really lost as to how to process the string and then converting it to another data type. Thanks for the help!
4.16 LAB: Checker for integer string Instructor note: This zyLab counts as a homework grade and...
4.16 LAB: Checker for integer string Instructor note: This zyLab counts as a homework grade and is due by 11:59pm on 9Oct. Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: 1995 the output...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Write a Python program that splits, doubles, and reverse each input string entered. For each string,...
Write a Python program that splits, doubles, and reverse each input string entered. For each string, split it at the given integer, and then output it in format as shown below. Convert all stings entered to uppercase for output. Assume string length is greater than the split value. Refer to the sample output below. Sample Runs: Enter a string: holiday Enter the number to split on: 3 HOL-YADI-LOH-IDAY Enter a string: TATTARRATTAT Enter the number to split on: 6 TATTAR-TATTAR-RATTAT-RATTAT...
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...
Let s be a string that contains a simple mathematical expression, e.g., s = '1.5 +...
Let s be a string that contains a simple mathematical expression, e.g., s = '1.5 + 2.1-3' s = '10.0-1.6 + 3 - 1.4' The expression will have multiple operands. We don't know exactly how many operands there are in s. The operator will be one of the following: +, -. Write a function called plus_minus() that takes s as the input, interpret the expression, then evaluate and return the output. Note: The use of the eval() is not allowed...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT