3

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
4

1 回答 1

6

看起来字节顺序在您的字节数组中有点不寻常。即,它包含在大端词中,但具有低端词序。

如果您采用494d0152LE 形式,则数组中的字节序列为:

52 01 4d 49

当您期望4d 49 52 01清楚时,“低”字是4d 49按BE顺序排列的。高字也一样。

所以你可能会尝试修改你的代码:

QDataStream dataStream(header);
dataStream.setByteOrder(QDataStream::BigEndian);
...
quint16 low, high;
quint32 temp;
dataStream >> low >> high;
temp = (high << 32) | low;
于 2013-03-18T21:15:48.570 回答