This function doesnt work correctly for some inputs, So What is the mistake ?
All Projects Codes here : link
ps: I am using input that "bits.size()%8" equal to zero
QByteArray bitsToBytes(QBitArray bits) {
QByteArray bytes;
bytes.resize(bits.count()/8);
// Convert from QBitArray to QByteArray
for(int b=0; b<bits.count(); ++b)
bytes[b/8] = ( bytes.at(b/8) | ((bits[b]?1:0)<<(b%8)));
return bytes;
}
Topro algorithm should be correct as a whole. But my concert is with the test
bits[b]?1:0
.By default,
operator[] ( int i )
return "the bit at index position i as a modifiable reference" whileoperator[] ( int i ) const
return a boolean. If the first definition is chozen, you will test if a reference is true.Try Topro algorithm with
bits.testBit(b)
.