Search code examples
arraysqtqbytearray

convert QBytearray to QBitarray


How can i convert a QBytearray to a QBitarray? Qt manual says this:

for(int i=0; i<8; ++i) {
    for(int b=0; b<byteArray.count();b++) {
        bits.setBit( i*8+b, byteArray.at(i)&(1<<(7-b)) );
    }

But this causes a runtime error. Is there a better way to do this?


Solution

  • The loop seems strange it is indexing the byte array with i which goes to 8, but the byteArray might not have 8 elements. Just switching the loop limits should work:

    for(int i = 0; i < byteArray.count(); ++i) {
      for(int b = 0; b < 8; b++) {
        bits.setBit( i * 8 + b, byteArray.at(i) & (1 << (7 - b)) );
    }