[Solved]QVector extract subvector
-
wrote on 12 Dec 2013, 07:47 last edited by
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 dataLike 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
-
wrote on 12 Dec 2013, 08:16 last edited by
Maybe something like
@data = datagram.mid(6);@ -
wrote on 12 Dec 2013, 08:20 last edited by
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.
-
wrote on 12 Dec 2013, 08:23 last edited by
You are welcome.
Please mark the thread solved by putting [Solved] in front of the title (to do that, edit your original post). -
wrote on 12 Dec 2013, 08:24 last edited by
[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.
-
wrote on 12 Dec 2013, 08:32 last edited by
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 -
wrote on 12 Dec 2013, 08:38 last edited by
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/7