C ++ Problem Padding 8 bits in a char

This is strange. This is a trivial problem:

A std :: string with bits in multiples of 8, first eighth: "10011100".

//Convert each 8 bits of encoded string to bytes
unsigned char c = 0;
for(size_t i = 0; i < encoded.size(); i += 8)
{
    for(size_t k = 0; k < 8; k++)
    {
        c <<= k;
        if(encoded.at(i + k) == '1') c += 1;

        //Debug for first 8 bits
        if(i == 0) cout << "at k = " << k << ", c = " << (int)c << endl;
    }
    outFile.write(reinterpret_cast<char*>(&c), sizeof(char));
}

      


Sets the output:

at k = 0, c = 1
at k = 1, c = 2
at k = 2, c = 8
at k = 3, c = 65
at k = 4, c = 17
at k = 5, c = 33
at k = 6, c = 64
at k = 7, c = 0

      

This makes no sense. Moving to 2 places from the left and getting 8 out of 2 is not possible. The maximum value it can have should be 111b = 7d and in this case should be 100b = 4d.

Enlighten me.

0


source to share


1 answer


at k = 0, c = 1
at k = 1, c = 2
at k = 2, c = 8

      

It's because:

input = 10011100
c = 0

`k=0, b=1` shift by 0 add 1 => `c = 1`, dec = 1
`k=1, b=0` shift by 1 add 0 => `c = 10`, dec = 2
`k=2, b=0` shift by 2 add 0 => `c = 1000`, dec = 8

      

b means "current bit". You might not want to navigate to k

, but to 1

? If you are looking for a standard C ++ solution, you can use std::bitset

:



std::bitset<8> bits("10011100");
unsigned char c = bits.to_ulong();

      

To output to a stream, you can use the function put

:

outFile.put(c);

      

It avoids cast pointers and also outputs unformatted (parameters such as field width are ignored).

+5


source







All Articles