PDA

View Full Version : QByteArray appending quint32 not working



TheNewGuy
30th March 2010, 23:17
I have the following code:




quint32 num = 17;

QByteArray ba;
ba.append(num);

qDebug() << "BA Size: " << ba.size();


I expect the output to be 4, since the a quint32 is comprised of 4 bytes. However, the output is 1 byte.

So, my question is, how do I append a quint32 number?

TheNewGuy
30th March 2010, 23:26
Answer for anyone else in the future:



quint32 num = 17;
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << num;
qDebug() << "BA Size: " << ba.size();

viheaho
30th March 2010, 23:29
I suggest tat you try method setNum andse if it works better.

TheNewGuy
30th March 2010, 23:55
The datastream method above works great, but it is a little bulky.

The problem with setNum() is that it converts the number into a string.