Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I copy one QByteArray at n-index of another QByteArray?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wost
    wrote on last edited by
    #1

    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)?

    raven-worxR Taz742T 2 Replies Last reply
    0
    • W wost

      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)?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wost
        wrote on last edited by
        #3

        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.

        raven-worxR 1 Reply Last reply
        0
        • W wost

          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)?

          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by Taz742
          #4

          @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"
          

          Do what you want.

          1 Reply Last reply
          0
          • W wost

            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.

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

            @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?

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved