nilennoct
2015-01-02 23:48:26 +08:00
抛砖引玉==buffer也可以开大点,比如byteBuf[32],再拿个变量记录bufOffset。。。
``` c++
ofstream outputBinFile;
char byteBuf = '\0';
short bitOffset = 7;
while (data not output completely) {
byteBuf |= {your data here(0 or 1)} << bitOffset;
if (bitOffset == 0) {
outputBinFile.write(&byteBuf, 1);
byteBuf = '\0';
bitOffset = 7;
}
else {
--bitOffset;
}
}
// output the rest data
if (bitOffset < 7) {
outputBinFile.write(&byteBuf, 1);
}
```