Solved Bytes array to QByteArray.
-
@JonB
But uint8_t = 0..255 and char = -127..127. If I make encrypted as a char array I loose data. Do I miss something? -
@jenya7
Yes, you do "miss something"!Both
uint8_t
andchar
are a byte with 8 bits in it. The unsigned/signed are a matter of how something might interpret the byte/char, but they remain the same 8-bit pattern. No "data is lost" when you cast between them. Just trust me on this! :)When you cast you would be better using
reinterpret_cast<>()
thanstatic_cast <>()
. -
@JonB
This wayData.append( QByteArray( static_cast<char*>(encrypted), MIN_CRYPT_SIZE ) );
I get - static_cast from 'uint8_t *' (aka 'unsigned char *') to 'char *' is not allowed
-
@JonB said in Bytes array to QByteArray.:
When you cast you would be better using
reinterpret_cast<>()
thanstatic_cast <>()
.I see. Thank you. It works.
-
@jenya7 you would need reinterpret cast and an additional const_cast.
You shouldn't be doing that anyway, use memcpy. More verbose to write, but more compiler robust, and probably just as fast.
-
-
Data.append( reinterpret_cast<char*>(encrypted), MIN_CRYPT_SIZE );
using QByteArray &QByteArray::append(const char *str, int len) is least typing.
Or since you don't use the
QByteArray Data;
before this, and so don't needappend()
, you could just move the declaration to here:QByteArray Data( reinterpret_cast<char*>(encrypted), MIN_CRYPT_SIZE );
-
@jenya7 said in Bytes array to QByteArray.:
memcpy(Data.data(), encrypted, MIN_CRYPT_SIZE);
I like it better.What? Please don't go into using
memcpy()
when no need, this code does not append anything (you were appending), or does not set the size (will overflow unallocated memory) in theQByteArray
(unless pre-sized). -
@jenya7 said in Bytes array to QByteArray.:
memcpy(Data.data(), encrypted, MIN_CRYPT_SIZE);
I like it better.
This is very dangerous!
You have to ensure that buffer holden byData
is big enough to write the data (Data.size() >= MIN_CRYPT_SIZE) or you will corrupt memory!EDIT
My suggestion for your code is:void UDP_Send(QByteArray data, QString remote_ip, quint16 remote_port) { QByteArray encrypted(CRYPT_SIZE, 0); CRYPTO_Encrypt(reinterpret_cast<uint8_t *>(encrypted.data()), encrypted.size(), reinterpret_cast<uint8_t *>(data.data()), data.size()); udp_socket->writeDatagram(encrypted, QHostAddress(remote_ip), remote_port); }
-
@KroMignon
I see. Thank you.
I thought aboutData.resize(CRYPT_SIZE);
But this way it's less coding.
-
@JonB said in Bytes array to QByteArray.:
@jenya7
Yes, you do "miss something"!Both
uint8_t
andchar
are a byte with 8 bits in it. The unsigned/signed are a matter of how something might interpret the byte/char, but they remain the same 8-bit pattern. No "data is lost" when you cast between them. Just trust me on this! :)When you cast you would be better using
reinterpret_cast<>()
thanstatic_cast <>()
.Following the logic.
Slave sends (not Qt, embedded code)float fval = SensorGetValue(); ival = (int)fval; udp_tx_buffer[5] = ival; udp_tx_buffer[6] = ival >> 8; udp_tx_buffer[7] = ival >> 16; udp_tx_buffer[8] = ival >> 24;
On Qt side I get
float fval = static_cast<float>( (sens_msg->data[3] << 24) | (sens_msg->data[2] << 16) | (sens_msg->data[1]<< 8) | sens_msg->data[0]);
But I get it truncated to integer.
-
@jenya7 said in Bytes array to QByteArray.:
On Qt side I get
float fval = static_cast<float>( (sens_msg->data[3] << 24) | (sens_msg->data[2] << 16) | (sens_msg->data[1]<< 8) | sens_msg->data[0]);But I get it truncated to integer.
This totally wrong,
sens_msg->data[]
is a 8 bit value, so shifting it to left with 8 bit (or more) will fill it with '0'.
You could cast each byte to an integer (before shifting) or useQDataStream
:QDataStream stream(&sens_msg, QIODevice::ReadOnly); float fval; stream >> fval;
cf. documentation https://doc.qt.io/qt-5/qdatastream.html.
-
@jenya7
If your sender & receiver are on same architecture, so you don't care about any possible byte-ordering issues (I don't know whether they apply tofloat
-types anyway), you could just send 4 bytes from&udp_tx_buffer[5]
and receive as 4 bytes into&fval
. No individual bytes. -
OK, I'll try it.