PDA

View Full Version : split QByteArray



xproch13
29th October 2010, 19:05
Hi, I have quite large QByteArray and need to split it in smaller (same sized) QByteArrays and possible create QList of these smaller QByteArrays. Let say I have QByteArray with 10 000 bytes and need to split this array to 19 QByteArrays each containing 512 bytes/chars and 1 last QByteArray with remaining bytes...

I have tried memcpy but it did not work for some reason. Can you give me some hint with example how to do it easily? Initial large QByteArray has not fixed size (ie it's size may vary) so number of QByteArrays has to be set dynamicaly. Size of smaller QbyteArrays may be fixed.
Thx

wysota
29th October 2010, 19:27
int pos = 0, arrsize = ba.size(), sizeInArray = 512;
QList<QByteArray> arrays;
while(pos<arrsize){
QByteArray arr = ba.mid(pos, sizeInArray);
arrays << arr;
pos+=arr.size();
}

xproch13
29th October 2010, 21:50
Huh, simple and great. Thanks