简体   繁体   English

如何在C ++中将表示为位串的字节写入文件?

[英]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. 当我查看文件的位转储时,不要写字符串"10010010" ,而0b10010010

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: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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