I want to write a QString 'str' into QByteArray 'baData'.
The 'baData' is having fixed size 80 Bytes (it's requirment).
If the 'str' size is less than 80, append the remaining data of 'baData' with 0 (zero) value.
I wrote following code, but not working porperly.
void MyClass::CopyData(QByteArray &packet)
{
packet.truncate(0);
packet[0] = 0x12;
packet[1] = 0x34;
....
QByteArray baData;
baData.resize(80);
baData = 0;
QString str = "Hello Wrold";
baData = str.toLocal8Bit();
packet.append(baData, 80);
}
packet size is not fixed, but baData size if fixed i.e. 80.
The following works for me:
QByteArray baData;
baData.fill(0, 80);
QString str = "Hello World";
baData.insert(0, str.toLocal8Bit());
baData.resize(80);
Basically how it works is, you fill the QByteArray
with 80 bytes of 0s, insert your data in the beginning and then resize it back to 80 bytes.