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 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!
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.
Assuming nine-bit 2’s complement representation, convert the decimal number -137 to binary, show the effect of...
Assuming nine-bit 2’s complement representation, convert the decimal number -137 to binary, show the effect of the ROL operation on it with initial carry C=1, show the status bits and then convert the result back to decimal. Repeat with the ASR operation. Write the RTL specification of both operations shown here.
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
17.19 Lab 8B: printing a binary number This lab asks you to use string formatting to...
17.19 Lab 8B: printing a binary number This lab asks you to use string formatting to print a binary number. First you will prompt the user for an integer, using exactly the prompt "Enter a decimal integer: " including the colon character and a space following it. Then print out the decimal number and it's binary equivalent. You do not need to do any mathematical conversion of the integer to binary, you can use the format method. For example, if...
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 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...
JAVA: Write a method with the following header to return a string format to represent the...
JAVA: Write a method with the following header to return a string format to represent the reverse order of the integer: public static String reverse(int number) For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal. Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
Write a program that takes a string of characters (including spaces) as input, computes the frequency...
Write a program that takes a string of characters (including spaces) as input, computes the frequency of each character, sorts them by frequency, and outputs the Huffman code for each character.   When you are writing your program, you should first test it on a string of 7 characters, so you can check it. PLEASE NOTE: Your program must work for any text that has upper and lower case letters digits 0 - 9, commas, periods, and spaces. Please submit the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT