C++ reads in binary Numbers and converts them to decimal output
- 2020-04-02 03:01:39
- OfStack
Topic describes
Given a binary number containing only 0 and 1 with a length not greater than 10, convert it to decimal and output it.
The input describing
Enter a binary integer n with a length not greater than 10
Output description
Output converted decimal number on one line
The sample input
110
Sample output
6
Solution:
Many people who have studied C and are just beginning to learn C++ may think of reading in a character array and then counting bits and bits into a decimal output.
There is no need.
The C++ class library provides a binary data class and its methods can be converted to decimal.
The code is as follows:
#include <iostream>
using namespace std;
#include <bitset>
int main()
{
bitset<16> bint; //16 bits of binary data, and bitset<32> < br / >
cin >> bint;
cout << bint.to_ulong() << endl;
return 0;
}