Reputation: 483
I use this simple code to convert a decimal into binary :
#include <iostream>
#include <windows.h>
#include <bitset>
using namespace std;
int main(int argc, char const *argv[]){
unsigned int n;
cout << "# Decimal: "; cin >> n; cout << endl;
bitset<16>binary(n);
cout << endl << "# Binary: " << binary << endl;
system("Pause"); return 0;
}
How to convert "binary" into decimal and assign the value to other variable ?
Upvotes: 0
Views: 8189
Reputation: 385144
n
is not "a decimal". I think you have a misconception of what numbers are, based on the default output representation used by IOStreams. They are numbers. Not decimal strings, binary strings, hexadecimal strings, octal strings, base-64 strings, or any kind of strings. But numbers.
The way you choose to represent them on output is entirely orthogonal to the way they are stored internally (which is, actually, base-2 not decimal), so it is highly likely that these "conversions" you're trying to do are inappropriate.
However, if you wish to extract an integer from a std::bitset
instance, you may do so using the to_ulong()
member function.
Get into the habit of using the documentation.
Upvotes: 2