How can I copy one QByteArray at n-index of another QByteArray?



  • Say I have two QByteArrays: one (a) at 136 bytes, and the other (b) at 128 bytes. I want to copy the entirety of (b) starting at (a)[6]. Is that possible, or would I have to do something like: (a) = (a).mid(0, 6) + (b) + (a).mid(133, 2)?


  • Moderators

    @wost
    when you use QDataStream for reading and writing the nested QByteArray data, Qt takes care of all for you.
    So that the nested QByteArrays have the correct size after reading them out again.



  • Will that work with readyRead? I'm getting the data through a QSerialPort.
    Doing this: _frame = _frame.left(6) + _states + _frame.right(2); worked, but I'm not sure if this is the best way to do it.



  • @wost

        QByteArray *arr = new QByteArray("abcd");
    
        QByteArray *arr2 = new QByteArray("efgh");
    
        QByteArray *arr3 = new QByteArray((QByteArray)(arr->mid(0, 2) + arr2->mid(0,3)));
    
        qDebug() << QString::fromStdString(arr3->toStdString()); = "abefg"
    

  • Moderators

    @wost said in How can I copy one QByteArray at n-index of another QByteArray?:

    Will that work with readyRead? I'm getting the data through a QSerialPort.

    so how do you ensure that you have already received the whole data you are looking for?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.