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. [Solved]QVector extract subvector
Forum Updated to NodeBB v4.3 + New Features

[Solved]QVector extract subvector

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 3.2k 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.
  • T Offline
    T Offline
    TOAOMatis
    wrote on 12 Dec 2013, 07:47 last edited by
    #1

    Hi all,

    currently I'm developing an UdpSocketListener, which snoops udp broadcasts from the network.
    The data is received and the datagram is extracted. Within this datagram the following data is packed:
    4 bytes Message Transaction ID, this is cast to an ulong
    1 byte Protocol Version, used for distinguishing different versions
    1 byte data length, this indicates the data size embedded within this datagram. Between 0 and 134 bytes.
    0..134 bytes data

    Like I said, the datagram is received and put in a QVector<uchar>, then the MTID is read and the datasize is read. If there is data available, which is normally the case, this needs to be assigned to an QVector<uchar>.
    @QVector<uchar> AcpPacket::toPacket(QVector<uchar> datagram)
    {
    ulong tid = 0;
    uchar size = 0;
    uchar pver = 0;
    QVector<uchar> data;
    /* There must be an easier way to extract an ulong from 4 bytes of data /
    tid = datagram.takeFirst() << 24;
    tid += datagram.takeFirst() << 16;
    tid += datagram.takeFirst() << 8;
    tid += datagram.takeFirst() << 0;
    pVer = datagram.takeFirst();
    size = datagram.takeFirst();
    /
    While there is no header data left any more from the packet, the remaining bytes must be data */
    data = datagram();
    return data;
    }@
    What I would like to achieve is the following (pseudo code)
    @tid = datagram.at(0).toLong();
    pVer = datagram.at(4);
    size = datagram.at(5);
    data = datagram[6]...datagram[6+size];
    @

    Is this possible? I come from POSIX C, where you can achieve this with pointer arithmetic, but I'm struggling with the Qt language.

    Many thanks in advance!

    TOAOMatis

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on 12 Dec 2013, 08:16 last edited by
      #2

      Maybe something like
      @data = datagram.mid(6);@

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TOAOMatis
        wrote on 12 Dec 2013, 08:20 last edited by
        #3

        You sir (ma'am), are a genius!
        Many thanks. I don know why I've missed this in the help files.

        The only problem remains; how can I cast the 4 bytes to a long.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on 12 Dec 2013, 08:23 last edited by
          #4

          You are welcome.
          Please mark the thread solved by putting [Solved] in front of the title (to do that, edit your original post).

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TOAOMatis
            wrote on 12 Dec 2013, 08:24 last edited by
            #5

            [quote author="Asperamanca" date="1386836587"]You are welcome.
            Please mark the thread solved by putting [Solved] in front of the title (to do that, edit your original post).[/quote]

            Yes, I will do. But I have another question pending (I've edited my previous post), how to cast 4 bytes from the QVector to an ulong.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on 12 Dec 2013, 08:32 last edited by
              #6

              QVector doesn't offer any specific support for that (as far as I know). However, this can be done using regular C/C++
              I found a discussion about that "here":http://stackoverflow.com/questions/14591763/c-2-char-array-elements-into-short-int

              1 Reply Last reply
              0
              • T Offline
                T Offline
                TOAOMatis
                wrote on 12 Dec 2013, 08:38 last edited by
                #7

                Ok, thanks for your quick reply.

                I already do it that way;
                @tid = datagram.takeFirst() << 24;
                tid += datagram.takeFirst() << 16;
                tid += datagram.takeFirst() << 8;
                tid += datagram.takeFirst() << 0;@

                Will mark this thread as resolved :-)

                1 Reply Last reply
                0

                1/7

                12 Dec 2013, 07:47

                • Login

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