[英]How to write a byte represented as a bitstring to a file in c++?
例如,我有一個
string str = "10010010";
如何將其作為單個字節寫入文件?
當我查看文件的位轉儲時,不要寫字符串"10010010"
,而0b10010010
。
這樣的事情應該起作用:
std::string str = "10010010";
uint8_t byte = static_cast<uint8_t>(std::bitset<8>(str).to_ulong() & 0xFFul);
file.write(&byte,1);
參考文獻:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.