Question

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.

Homework Answers

Answer #1
#include <iostream>
#include <string>

using namespace std;

int main() {
    string s;
    cout << "Enter a binary number: ";
    cin >> s;
    long d = 0;
    for (int i = 0; i < s.size(); ++i) {
        d *= 2;
        d += s[i] - '0';
    }
    cout << s << " in decimal is " << d << endl;
    return 0;
}

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
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 =...
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...
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!
Convert a 10 digit binary number that has 3 decimal digits (of your own choosing) to...
Convert a 10 digit binary number that has 3 decimal digits (of your own choosing) to decimal (show all steps) . Find the twos complement of the following number: (show all steps) 88 decimals (first convert to binary)
IN C++ Write a program named printStringReverse.cpp to read in a C-Style sting. Print the string...
IN C++ Write a program named printStringReverse.cpp to read in a C-Style sting. Print the string forward and then backward using pointer increment and decrement. Declare the C-Style string as following: char s[50]; Read the string as follows; cin >> s; Write the following code to get the actual number of characters from your string. int count=0; while (s[count] != NULL) { count++; } count contains the actual length of your string since we declared the array to hold a...
C++ Part B: (String) Program Description: Write a word search program that searches an input data...
C++ Part B: (String) Program Description: Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following functions : void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount) ; (15%)...
Find the 2's complement of the following decimal numbers (You must convert the number to binary...
Find the 2's complement of the following decimal numbers (You must convert the number to binary and the answer has to be in binary with the same number of bits as the conversion)? a. 20 b. 15 c. 2126 d. 266 e. 466 please explain how you did it!
Determine the number of digits needed to write the decimal number 289,259,165 in binary.
Determine the number of digits needed to write the decimal number 289,259,165 in binary.
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the equivalent radix-e (another arbitrary base) number. Where e and d are members in [2, 16]: Hints: The easiest approach is - you can convert the radix-d number to decimal one first, and then convert the decimal number to the radix-e number.
Write a recursive function count_digits that counts all the digits in a string. Program: C
Write a recursive function count_digits that counts all the digits in a string. Program: C