简体   繁体   中英

C++ ValueError bitset::_M_copy_from_ptr when string and bitset are correct size

I have a string which should represent a 32 bit integer, so i'm creating a bitset to print the bits:

std::string str = rocksDBSlice.ToString();
std::cout << str.length() << std::endl;
std::bitset<32> bits(str);
std::cout << bits.to_string() << std::endl;

But at run-time I get:

4
ValueError bitset::_M_copy_from_ptr

If the string length is 4 why does the bitset throw?

Constructor of std::bitset with string argument expects the string to contain only 0 s and 1 s. Probably your string contains values like a instead of 1100001 . So if you want the 32 bits of 4 bytes of your input string to be put into a bitset , you have to convert the string to a sequence of 32 ones and zeros first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM