Pergunta

I'm using QDatastream to populate a quint32 array from a QByteArray on an Little-Endian ARM machine like below:

    // Modify the header
    QByteArray header = QByteArray::fromRawData((const char*)dataPtr,HEADER_SIZE);
    QDataStream dataStream(header);
    dataStream.setByteOrder(QDataStream::LittleEndian);
    quint32 headerBuffer[NUMBER_HEADER_ENTRIES];
    int bufferCnt = 0;
    while ( !dataStream.atEnd() ) {

        // Pop off a 32 bit int
        quint32 temp;
        dataStream >> temp;

        // insert into buffer
        headerBuffer[bufferCnt] = temp;

        // Increment counter
        bufferCnt++;

    }

The problem is with byte order. I need to grab the last 4 bits of the 32bit field in headerBuffer[113], so I tried to AND that entry in headerBuffer with 0xf. The expected value to check against this field is "3". However, this AND operation gives me "a", as shown below. If I swap the bytes so that the entry is "0x1a13 & 0x000f", then I get "3". You can see below a few other examples of the values that are expected, versus what I am seeing. So, I am setting the ByteOrder of the QDataStream to LittleEndian, but still not getting the desired results. What am I doing wrong? How can I get 0x1a13 instead of 0x131a? Thanks!

        qDebug() << "ONE: " << QString("%1").arg(headerBuffer[0], 0, 16); // This prints: 494d0152 -- Should be: 4d495201
        qDebug() << "TWO: " << QString("%1").arg(headerBuffer[1], 0, 16); // This prints: 5400 -- Should be: 54
        qDebug() << "THREE: " << QString("%1").arg(headerBuffer[113] & 0x000f, 0, 16); // This prints: a -- Should be: 3 ( headerBuffer[113] is always 0x131a )
        qDebug() << "FOUR: " << QString("%1").arg(0x1a13 & 0x000f, 0, 16); // This prints: 3 -- Should be: 3
Foi útil?

Solução

Looks like the byte order is a bit unusual in your byte array. I.e. it is packed in big-endian words but with low-endian word order.

If you are getting 494d0152 in LE form, the byte sequence in array is:

52 01 4d 49

When you are expecting 4d 49 52 01 it it clear, that the 'low' word is 4d 49 is in BE order. Same for high word.

So you might try to modify your code:

QDataStream dataStream(header);
dataStream.setByteOrder(QDataStream::BigEndian);
...
quint16 low, high;
quint32 temp;
dataStream >> low >> high;
temp = (high << 32) | low;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top