简体   繁体   English

将CRC uint转换为QByteArray QT-C ++

[英]Convert CRC uint to QByteArray QT - C++

I've CRC value into uint that is: 我将CRC值转换为uint:

95DF 95DF

My target is return from uint two byte in QByteArray. 我的目标是从QByteArray中的uint两个字节返回。

I get this: 我得到这个:

`CRC uint 95DF `CRC uint 95DF

//in simple i should return this //简单地说,我应该返回这个

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

I've tryed convert uint to QString but this one change return value. 我尝试将uint转换为QString,但是这一更改返回值。 Ho to keep the result and return QByteArray ? 何保持结果并返回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: 您还可以使用QDataStream实现此目的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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