有时候需要将 QByteArray 容器中的内容进行倒置,用一行代码即可实现:
std::reverse(arr.begin(), arr.end());
示例代码
QByteArray arr;
arr.append(0x01);
arr.append(0x02);
arr.append(0x03);
arr.append(0x04);
qDebug() << "倒置前:" << arr.toHex(); // 01020304
std::reverse(arr.begin(), arr.end());
qDebug() << "倒置后:" << arr.toHex(); // 04030201
参考资料
- https://stackoverflow.com/questions/50257210/convert-qbytearray-from-big-endian-to-little-endian/50257560