Nic*_*ick 7 c++ format binary stringstream c++11
我需要解析std::string
包含二进制格式的数字,例如:
0b01101101
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用std :: hex格式说明符来解析十六进制格式的数字.
std::string number = "0xff";
number.erase(0, 2);
std::stringstream sstream(number);
sstream << std::hex;
int n;
sstream >> n;
Run Code Online (Sandbox Code Playgroud)
是否存在二进制格式的等价物?
Rev*_*lot 10
您可以使用std::bitset
字符串构造函数并将bistet转换为数字:
std::string number = "0b101";
//We need to start reading from index 2 to skip 0b
//Or we can erase that substring beforehand
int n = std::bitset<32>(number, 2).to_ulong();
//Be careful with potential overflow
Run Code Online (Sandbox Code Playgroud)