简体   繁体   中英

How to write a byte represented as a bitstring to a file in c++?

For example, I have a

string str = "10010010";

How do I write this as a single byte to a file?

Not to write string "10010010" , but to have 0b10010010 when I look at a bits dump of the file.

Something like this should work:

std::string str = "10010010";
uint8_t byte = static_cast<uint8_t>(std::bitset<8>(str).to_ulong() & 0xFFul);
file.write(&byte,1);

References:

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