Question

Write a C++ program that defines a function that converts a binary number string to an...

  1. Write a C++ program that defines a function that converts a binary number string to an integer. However, if the binary number is greater than or equal to 4,294,967,296, the function must return 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>

using namespace std;

unsigned int binary_to_decimal(string s) {
    if (s.length() > 32)
        return 0;
    unsigned int value = 0;
    for (int i = 0; i < s.size(); i++) {
        value *= 2;
        value += s[i] - '0';
    }
    return value;
}

int main() {
    string s;
    cout << "Enter a binary string: ";
    cin >> s;
    cout << s << " in decimal is " << binary_to_decimal(s) << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that defines a function that converts a binary number string to an...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write MIPS code that converts a string containing a number into an actual number. The function...

    Write MIPS code that converts a string containing a number into an actual number. The function to do this conversion, atoi (Array To Integer) should have a single input (the string) and output a single number (the integer). The Java/C code is as follows: atoi(a0: array) { int digit, number, i = 0; while( (array[i] >= '0') && (array[i] <= '9') ) { digit = array[i] - '0'; number = 10 * number + digit; i++; } return number; }

  • Python Programing : Convert to binary - functions Write a program that takes in a positive...

    Python Programing : Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a...

  • C++ 10.4 String to number Write a program that 1.reads in 4 upper case characters Converts...

    C++ 10.4 String to number Write a program that 1.reads in 4 upper case characters Converts each into its Ascii equivalent integer 3.Adds all these numbers together Converts this resulting integer to a string Prints the string. Divides the integer result by 4 Converts this back to an Ascii char prints the char

  • <Java> 1-Write a program that converts from a string into an integer number. For example: For...

    <Java> 1-Write a program that converts from a string into an integer number. For example: For "123" the program should return 123 For "131", The program should return 131 For "-121" the program should return -121 For "1e3" the program should print an Invalid number For "a1" the program should print an invalid number Do not use Integer.parse function 2. Generate a random number in [0, 100]. Write an efficient guess procedure that takes no more than 7 guesses to...

  • (C++ only) Write a function that returns a decimal number from a binary string. The function...

    (C++ only) Write a function that returns a decimal number from a binary string. The function header is as follows: int bin2Dec(const string& binaryString) For example, bin2Dec("10001") returns 17. Write a test program that prompts the user to enter a binary number as a string and displays its decimal equivalent value Sample Input: 1110100110101 Sample Output: Enter a bianry number: 1110100110101 7477

  • Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the...

    Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the program just the two functions for my C++ program. I wanted to post the sample code from my class but it won't allow me it's too long. This calculate shall have the following five functionalities: Provide sign extension for a binary number Provide two’s complement for a binary nunber string signed_extension(string b);              // precondition: s is a string that...

  • A) Write a function that receives an integer decimal number and converts it to its Hexadecimal...

    A) Write a function that receives an integer decimal number and converts it to its Hexadecimal equivalent B) Write a function that converts a binary number to its corresponding Decimal equivalent BOTH A AND B IN ARDUINO C LANGUAGE

  • In Java Write a program the takes in a number and converts it to binary. Use...

    In Java Write a program the takes in a number and converts it to binary. Use an OOP class approach using class and objects

  • (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers....

    (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers. Write a program that converts a positive integer into the Roman number system. The Roman number system has digits I 1 V 5 X 10 L 50 C 100 D 500 M 1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately....

  • How do I write a C program called binary that takes a single command line argument,...

    How do I write a C program called binary that takes a single command line argument, and integer, in decimal, and prints out the binary representation of the number with 64 bits. The argument must be stored as a long. Use atol to convert the command line argument. Include a function char *binary(long n, char *b) { ... } that stores the binary representation in the string b with '0's and '1's. It is the responsibility of the calling program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT