Fix typo in Endian and add uint{16,32,64}ToBytes

This commit is contained in:
angelsl 2017-11-12 20:58:41 +08:00 committed by Jonathan White
parent 2e19af5032
commit 23347b392f
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
2 changed files with 49 additions and 1 deletions

View File

@ -139,7 +139,7 @@ QByteArray int16ToBytes(qint16 num, QSysInfo::Endian byteOrder)
qToLittleEndian<qint16>(num, reinterpret_cast<uchar*>(ba.data()));
}
else {
qToBigEndian<qint64>(num, reinterpret_cast<uchar*>(ba.data()));
qToBigEndian<qint16>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
@ -175,6 +175,51 @@ QByteArray int64ToBytes(qint64 num, QSysInfo::Endian byteOrder)
return ba;
}
QByteArray uint16ToBytes(quint16 num, QSysInfo::Endian byteOrder)
{
QByteArray ba;
ba.resize(2);
if (byteOrder == QSysInfo::LittleEndian) {
qToLittleEndian<quint16>(num, reinterpret_cast<uchar*>(ba.data()));
}
else {
qToBigEndian<quint16>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
}
QByteArray uint32ToBytes(quint32 num, QSysInfo::Endian byteOrder)
{
QByteArray ba;
ba.resize(4);
if (byteOrder == QSysInfo::LittleEndian) {
qToLittleEndian<quint32>(num, reinterpret_cast<uchar*>(ba.data()));
}
else {
qToBigEndian<quint32>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
}
QByteArray uint64ToBytes(quint64 num, QSysInfo::Endian byteOrder)
{
QByteArray ba;
ba.resize(8);
if (byteOrder == QSysInfo::LittleEndian) {
qToLittleEndian<quint64>(num, reinterpret_cast<uchar*>(ba.data()));
}
else {
qToBigEndian<quint64>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
}
bool writeInt16(qint16 num, QIODevice* device, QSysInfo::Endian byteOrder)
{
QByteArray ba = int16ToBytes(num, byteOrder);

View File

@ -42,6 +42,9 @@ namespace Endian {
QByteArray int16ToBytes(qint16 num, QSysInfo::Endian byteOrder);
QByteArray int32ToBytes(qint32 num, QSysInfo::Endian byteOrder);
QByteArray int64ToBytes(qint64 num, QSysInfo::Endian byteOrder);
QByteArray uint16ToBytes(quint16 num, QSysInfo::Endian byteOrder);
QByteArray uint32ToBytes(quint32 num, QSysInfo::Endian byteOrder);
QByteArray uint64ToBytes(quint64 num, QSysInfo::Endian byteOrder);
bool writeInt16(qint16 num, QIODevice* device, QSysInfo::Endian byteOrder);
bool writeInt32(qint32 num, QIODevice* device, QSysInfo::Endian byteOrder);