- 按照约定的大小端顺序,在数组中取出指定数据,并转为指令的大小端字节序的一些方法
qint8 extractqInt8(const QByteArray &byteArray, int startPos)
{if (startPos >= byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const char *data = byteArray.constData() + startPos;qint8 value = static_cast<qint8>(data[0]);return value;
}quint8 extractqUint8(const QByteArray &byteArray, int startPos)
{if (startPos >= byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const char *data = byteArray.constData() + startPos;quint8 value = static_cast<quint8>(data[0]);return value;
}
quint16 extractqUint16_BE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 2 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);quint16 value = static_cast<quint16>(data[1])| (static_cast<quint16>(data[0]) << 8);return value;
}
quint32 extractqUint32_BE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 4 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);quint32 value = static_cast<quint32>(data[3])| (static_cast<quint32>(data[2]) << 8)| (static_cast<quint32>(data[1]) << 16)| (static_cast<quint32>(data[0]) << 24);return value;
}
quint64 extractqUint64_BE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 8 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);quint64 value = static_cast<quint64>(data[7])| (static_cast<quint64>(data[6]) << 8)| (static_cast<quint64>(data[5]) << 16)| (static_cast<quint64>(data[4]) << 24)| (static_cast<quint64>(data[3]) << 32)| (static_cast<quint64>(data[2]) << 40)| (static_cast<quint64>(data[1]) << 48)| (static_cast<quint64>(data[0]) << 56);return value;
}
qint16 extractqInt16_BE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 2 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);qint16 value = static_cast<qint16>(data[1])| (static_cast<qint16>(data[0]) << 8);return value;
}
qint32 extractqInt32_BE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 4 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);qint32 value = static_cast<qint32>(data[3])| (static_cast<qint32>(data[2]) << 8)| (static_cast<qint32>(data[1]) << 16)| (static_cast<qint32>(data[0]) << 24);return value;
}
qint64 extractqInt64_BE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 8 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);qint64 value = static_cast<qint64>(data[7])| (static_cast<qint64>(data[6]) << 8)| (static_cast<qint64>(data[5]) << 16)| (static_cast<qint64>(data[4]) << 24)| (static_cast<qint64>(data[3]) << 32)| (static_cast<qint64>(data[2]) << 40)| (static_cast<qint64>(data[1]) << 48)| (static_cast<qint64>(data[0]) << 56);return value;
}
quint16 extractqUint16_LE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 2 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}quint16 littleEndianValue;std::memcpy(&littleEndianValue, byteArray.constData() + startPos, sizeof(quint16));return littleEndianValue;
}
quint32 extractqUint32_LE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 4 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}quint32 littleEndianValue;std::memcpy(&littleEndianValue, byteArray.constData() + startPos, sizeof(quint32));return littleEndianValue;
}
quint64 extractqUint64_LE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 8 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}quint64 littleEndianValue;std::memcpy(&littleEndianValue, byteArray.constData() + startPos, sizeof(quint64));return littleEndianValue;
}
qint16 extractqInt16_LE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 2 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}qint16 littleEndianValue;std::memcpy(&littleEndianValue, byteArray.constData() + startPos, sizeof(qint16));return littleEndianValue;
}
qint32 extractqInt32_LE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 4 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}qint32 littleEndianValue;std::memcpy(&littleEndianValue, byteArray.constData() + startPos, sizeof(qint32));return littleEndianValue;
}
qint64 extractqInt64_LE2LE(const QByteArray &byteArray, int startPos)
{if (startPos + 8 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}qint64 littleEndianValue;std::memcpy(&littleEndianValue, byteArray.constData() + startPos, sizeof(qint64));return littleEndianValue;
}
quint16 extractqUint16_BE2BE(const QByteArray &byteArray, int startPos)
{if (startPos + 2 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);quint16 littleEndianValue = static_cast<quint16>(data[1])| (static_cast<quint16>(data[0]) << 8);quint16 bigEndianValue = (littleEndianValue >> 8)| (littleEndianValue << 8);return bigEndianValue;
}
quint32 extractqUint32_BE2BE(const QByteArray &byteArray, int startPos)
{if (startPos + 4 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);quint32 littleEndianValue = static_cast<quint32>(data[3])| (static_cast<quint32>(data[2]) << 8)| (static_cast<quint32>(data[1]) << 16)| (static_cast<quint32>(data[0]) << 24);quint32 bigEndianValue = ((littleEndianValue & 0x000000FF) << 24)| ((littleEndianValue & 0x0000FF00) << 8)| ((littleEndianValue & 0x00FF0000) >> 8)| ((littleEndianValue & 0xFF000000) >> 24);return bigEndianValue;
}
quint64 extractqUint64_BE2BE(const QByteArray &byteArray, int startPos)
{if (startPos + 8 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);quint64 littleEndianValue = static_cast<quint64>(data[7])| (static_cast<quint64>(data[6]) << 8)| (static_cast<quint64>(data[5]) << 16)| (static_cast<quint64>(data[4]) << 24)| (static_cast<quint64>(data[3]) << 32)| (static_cast<quint64>(data[2]) << 40)| (static_cast<quint64>(data[1]) << 48)| (static_cast<quint64>(data[0]) << 56);quint64 bigEndianValue = ((littleEndianValue & 0x00000000000000FFULL) << 56)| ((littleEndianValue & 0x000000000000FF00ULL) << 40)| ((littleEndianValue & 0x0000000000FF0000ULL) << 24)| ((littleEndianValue & 0x00000000FF000000ULL) << 8)| ((littleEndianValue & 0x000000FF00000000ULL) >> 8)| ((littleEndianValue & 0x0000FF0000000000ULL) >> 24)| ((littleEndianValue & 0x00FF000000000000ULL) >> 40)| ((littleEndianValue & 0xFF00000000000000ULL) >> 56);return bigEndianValue;
}
qint16 extractqInt16_BE2BE(const QByteArray &byteArray, int startPos)
{if (startPos + 2 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);qint16 littleEndianValue = static_cast<qint16>(data[1])| (static_cast<qint16>(data[0]) << 8);qint16 bigEndianValue = (littleEndianValue >> 8)| (littleEndianValue << 8);return bigEndianValue;
}
qint32 extractqInt32_BE2BE(const QByteArray &byteArray, int startPos)
{if (startPos + 4 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);qint32 littleEndianValue = static_cast<qint32>(data[3])| (static_cast<qint32>(data[2]) << 8)| (static_cast<qint32>(data[1]) << 16)| (static_cast<qint32>(data[0]) << 24);qint32 bigEndianValue = ((littleEndianValue & 0x000000FF) << 24)| ((littleEndianValue & 0x0000FF00) << 8)| ((littleEndianValue & 0x00FF0000) >> 8)| ((littleEndianValue & 0xFF000000) >> 24);return bigEndianValue;
}
qint64 extractqInt64_BE2BE(const QByteArray &byteArray, int startPos)
{const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);qint64 littleEndianValue = static_cast<qint64>(data[7])| (static_cast<qint64>(data[6]) << 8)| (static_cast<qint64>(data[5]) << 16)| (static_cast<qint64>(data[4]) << 24)| (static_cast<qint64>(data[3]) << 32)| (static_cast<qint64>(data[2]) << 40)| (static_cast<qint64>(data[1]) << 48)| (static_cast<qint64>(data[0]) << 56);qint64 bigEndianValue = ((littleEndianValue & 0x00000000000000FFULL) << 56)| ((littleEndianValue & 0x000000000000FF00ULL) << 40)| ((littleEndianValue & 0x0000000000FF0000ULL) << 24)| ((littleEndianValue & 0x00000000FF000000ULL) << 8)| ((littleEndianValue & 0x000000FF00000000ULL) >> 8)| ((littleEndianValue & 0x0000FF0000000000ULL) >> 24)| ((littleEndianValue & 0x00FF000000000000ULL) >> 40)| ((littleEndianValue & 0xFF00000000000000ULL) >> 56);return bigEndianValue;
}
quint16 extractqUint16_LE2BE(const QByteArray &byteArray, int startPos)
{if (startPos + 2 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);quint16 littleEndianValue = static_cast<quint16>(data[0])| (static_cast<quint16>(data[1]) << 8);quint16 bigEndianValue = (littleEndianValue >> 8)| (littleEndianValue << 8);return bigEndianValue;
}
quint32 extractqUint32_LE2BE(const QByteArray &byteArray, int startPos)
{if (startPos + 4 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);quint32 littleEndianValue = static_cast<quint32>(data[0])| (static_cast<quint32>(data[1]) << 8)| (static_cast<quint32>(data[2]) << 16)| (static_cast<quint32>(data[3]) << 24);quint32 bigEndianValue = ((littleEndianValue & 0x000000FF) << 24)| ((littleEndianValue & 0x0000FF00) << 8)| ((littleEndianValue & 0x00FF0000) >> 8)| ((littleEndianValue & 0xFF000000) >> 24);return bigEndianValue;
}
quint64 extractqUint64_LE2BE(const QByteArray &byteArray, int startPos)
{if (startPos + 8 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);quint64 littleEndianValue = static_cast<quint64>(data[0])| (static_cast<quint64>(data[1]) << 8)| (static_cast<quint64>(data[2]) << 16)| (static_cast<quint64>(data[3]) << 24)| (static_cast<quint64>(data[4]) << 32)| (static_cast<quint64>(data[5]) << 40)| (static_cast<quint64>(data[6]) << 48)| (static_cast<quint64>(data[7]) << 56);quint64 bigEndianValue = ((littleEndianValue & 0x00000000000000FFULL) << 56)| ((littleEndianValue & 0x000000000000FF00ULL) << 40)| ((littleEndianValue & 0x0000000000FF0000ULL) << 24)| ((littleEndianValue & 0x00000000FF000000ULL) << 8)| ((littleEndianValue & 0x000000FF00000000ULL) >> 8)| ((littleEndianValue & 0x0000FF0000000000ULL) >> 24)| ((littleEndianValue & 0x00FF000000000000ULL) >> 40)| ((littleEndianValue & 0xFF00000000000000ULL) >> 56);return bigEndianValue;
}
qint16 extractqInt16_LE2BE(const QByteArray &byteArray, int startPos)
{if (startPos + 2 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);qint16 littleEndianValue = static_cast<qint16>(data[0])| (static_cast<qint16>(data[1]) << 8);qint16 bigEndianValue = (littleEndianValue >> 8)| (littleEndianValue << 8);return bigEndianValue;
}
qint32 extractqInt32_LE2BE(const QByteArray &byteArray, int startPos)
{if (startPos + 4 > byteArray.size()) {qWarning() << "Invalid start position or insufficient data.";return 0;}const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);qint32 littleEndianValue = static_cast<qint32>(data[0])| (static_cast<qint32>(data[1]) << 8)| (static_cast<qint32>(data[2]) << 16)| (static_cast<qint32>(data[3]) << 24);qint32 bigEndianValue = ((littleEndianValue & 0x000000FF) << 24)| ((littleEndianValue & 0x0000FF00) << 8)| ((littleEndianValue & 0x00FF0000) >> 8)| ((littleEndianValue & 0xFF000000) >> 24);return bigEndianValue;
}
qint64 extractqInt64_LE2BE(const QByteArray &byteArray, int startPos)
{const uchar *data = reinterpret_cast<const uchar *>(byteArray.constData() + startPos);qint64 littleEndianValue = static_cast<qint64>(data[0])| (static_cast<qint64>(data[1]) << 8)| (static_cast<qint64>(data[2]) << 16)| (static_cast<qint64>(data[3]) << 24)| (static_cast<qint64>(data[4]) << 32)| (static_cast<qint64>(data[5]) << 40)| (static_cast<qint64>(data[6]) << 48)| (static_cast<qint64>(data[7]) << 56);qint64 bigEndianValue = ((littleEndianValue & 0x00000000000000FFULL) << 56)| ((littleEndianValue & 0x000000000000FF00ULL) << 40)| ((littleEndianValue & 0x0000000000FF0000ULL) << 24)| ((littleEndianValue & 0x00000000FF000000ULL) << 8)| ((littleEndianValue & 0x000000FF00000000ULL) >> 8)| ((littleEndianValue & 0x0000FF0000000000ULL) >> 24)| ((littleEndianValue & 0x00FF000000000000ULL) >> 40)| ((littleEndianValue & 0xFF00000000000000ULL) >> 56);return bigEndianValue;
}