12

It takes 4 bytes to represent an integer. How can I store an int in a QByteArray so that it only takes 4 bytes?

  • QByteArray::number(..) converts the integer to string thus taking up more than 4 bytes.
  • QByteArray((const char*)&myInteger,sizeof(int)) also doesn't seem to work.
msrd0
  • 7,816
  • 9
  • 47
  • 82
c0dehunter
  • 6,412
  • 16
  • 77
  • 139
  • Actually, te second solution works :) You just need to cast it back properly. Will post an answer when timer will allow it thus helping others (I'm seeing a lot of this question everywhere, nowhere properly resolved in a SO manner :)) – c0dehunter Dec 02 '12 at 11:32
  • I know this is kind of old, but what do you mean by `cast it properly`? I've been trying to cast it properly for the last 2 hours.. :D – Itay Grudev May 24 '15 at 08:01

3 Answers3

21

There are several ways to place an integer into a QByteArray, but the following is usually the cleanest:

QByteArray byteArray;
QDataStream stream(&byteArray, QIODevice::WriteOnly);

stream << myInteger;

This has the advantage of allowing you to write several integers (or other data types) to the byte array fairly conveniently. It also allows you to set the endianness of the data using QDataStream::setByteOrder.

Update

While the solution above will work, the method used by QDataStream to store integers can change in future versions of Qt. The simplest way to ensure that it always works is to explicitly set the version of the data format used by QDataStream:

QDataStream stream(&byteArray, QIODevice::WriteOnly);
stream.setVersion(QDataStream::Qt_5_10); // Or use earlier version

Alternately, you can avoid using QDataStream altogether and use a QBuffer:

#include <QBuffer>
#include <QByteArray>
#include <QtEndian>

...

QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
myInteger = qToBigEndian(myInteger); // Or qToLittleEndian, if necessary.
buffer.write((char*)&myInteger, sizeof(qint32));
RA.
  • 7,542
  • 1
  • 34
  • 35
  • There's no guarantee in the doc that this will produce a 4-byte store in the byte array. – Stephen Chu Dec 03 '12 at 02:18
  • 3
    @StephenChu Under what circumstance would it not produce a 4-byte array (especially if `myInteger` is of type `qint32` or `quint32`)? – RA. Dec 03 '12 at 04:53
1

@Primož Kralj did not get around to posting a solution with his second method, so here it is:

int myInt = 0xdeadbeef;
QByteArray qba(reinterpret_cast<const char *>(&myInt), sizeof(int));

qDebug("QByteArray has bytes %s", qPrintable(qba.toHex(' ')));

prints:

QByteArray has bytes ef be ad de

on an x64 machine.

dqbydt
  • 73
  • 9
0

Recently I faced the same problem with a little variation. I had to store a vector of unsigned short into QByteArray. The trick with QDataStream did not work for unknown reason. So, my solution is:

QVector<uint16_t> d={1,2,3,4,5};
QByteArray dd((char*)d.data(),d.size()*sizeof(uint16_t));

The way to get the vector back is:

QVector<uint16_t> D;
for(int i=0; i<dd.size()/sizeof(uint16_t); ++i){
  D.push_back(*(uint16_t*)(dd.data()+i*sizeof(uint16_t)) );
}
Alex
  • 43
  • 5