怎样将string转化为2进制数呢?
怎样将得到的string(如:1011001110001010110010101111100000之类)转化为2进制数呢?而不是用atoi把它转化。因为1010转化为2进制数是10,而用atoi转化则为一千零十。
[解决办法]
- C/C++ code
std::string strData = "1010"; std::bitset<32> bitData(strData); unsigned long lData = bitData.to_ulong();
[解决办法]
from msdn:
- C/C++ code
// bitset_to_ulong.cpp// compile with: /EHsc#include <bitset>#include <iostream>int main( ){ using namespace std; bitset<5> b1 ( 7 ); cout << "The ordered set of bits in the bitset<5> b1( 7 )" << "\n that was generated by the number 7 is: ( " << b1 << " )" << endl; unsigned long int i; i = b1.to_ulong( ); cout << "The integer returned from the bitset b1," << "\n by the member function to_long( ), that" << "\n generated the bits as a base two number is: " << i << "." << endl;}