1

I have a large (122k length) string of 0s and 1s (010011101...) that needs to be written to a binary file as 0s and 1s and not their character representations.

What I've tried:

  • Outputting a Binary String to a Binary File in C++ - This is a good solution, however, I will have multiple strings of varying sizes and bitset needs a size at runtime as far as I know.
  • Stanford C++ library has a writebit function which works, but this takes way, way too long since each bit opens a write function.
  • different ways of implementing outputfile.write(), but they all write the character representations of the 0s and 1s.

Ideally, I'd rather use standard libraries. Thank you for any help in advance.

  • For the first 8 of you 0s and 1s, do you expect eight bytes written, some with value 0, some with value 1? Or a single byte with the value represnted by the first 8 of your 0/1s, i.e. a value between 0 and 255, written to one byte? – Yunnosch Sep 26 '19 at 18:16
  • The second option. – southwickIO Sep 26 '19 at 18:28

1 Answers1

2

You can combine eight bits each into one character:

int n = 0;
uint8_t value = 0;
for(auto c : str)
{
    value |= static_cast<unint8_t>(c == '1') << n;
    if(++n == 8)
    {
        // print value or buffer it elsewhere, if you want
        // to print greater chunks at once
        n = 0;
        value = 0;
    }
}
if(n != 0)
{
    // one partial byte left
}

Bytes have a fixed number of bits (eight usually), and you cannot just discard them, they will go into your file. So you need some way to specify, when decoding again, how many bits to discard. You might append an additional byte how many bits are valid in very last byte, you might encode total number of bits by some means (and could check if sufficent bytes have been read), ...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59