QbyteArray data copy

beparas picture beparas · Apr 1, 2014 · Viewed 17k times · Source

I have two QByteArray, sData and dData.

I want to copy n bytes from location x in dData i.e. &dData[x] to location y of sData i.e. &sData[y].

In C, array copy is done by memcpy(&dData[x], &sData[y], n);

How could copying above data of QByteArray be done in Qt?

Answer

TheDarkKnight picture TheDarkKnight · Apr 1, 2014

From the Qt documentation, you can use the replace function: -

QByteArray & QByteArray::replace(int pos, int len, const QByteArray & after)

Replaces len bytes from index position pos with the byte array after, and returns a reference to this byte array.

So, using the overload

QByteArray & QByteArray::replace(int pos, int len, const char * after);

sData = sData.replace(y, nBytes, dData.constData()+x);