简体   繁体   中英

Convert CRC uint to QByteArray QT - C++

I've CRC value into uint that is:

95DF

My target is return from uint two byte in QByteArray.

I get this:

`CRC uint 95DF

//in simple i should return this

QByteArray[0] = 95;
QByteArray[1] = DF;

I've tryed convert uint to QString but this one change return value. Ho to keep the result and return QByteArray ?

Thanks

unsigned int value = 0x95df;
char bytes[2] = {};
bytes[0] = (value >> 8) & 0xff;
bytes[1] = value & 0xff;
QByteArray qba(bytes, 2);

Alternatively:

unsigned int value = 0x95df;
value = qToBigEndian(value); // for x86 and little endian, this puts the bytes in expected order. no-op on big-endian
QByteArray qba((char*)&value, 2);

You can also use QDataStream to achieve this:

QByteArray qba;
QDataStream qbaStream(&seq,QIODevice::WriteOnly);
qbaStream << static_cast<quint16>(value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM