I'm not sure I understand how the QByteArray Object works as of yet (it's raw char data, right?), but here's my dilemma:
I'm trying to access data from a UDP datagram in a function. Code:
QByteArray buffer;
buffer.resize(udpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
This frees up the buffer for other incoming data and allows me to process my current datagram.
As far as I can tell, I must store the data in a QByteArray, because any other attempt has made the compiler very cross with me. Regardless, the data stored in the QByteArray is a series of unsigned 16 bit values(commands) that I need to access. Can it be read directly out of the QByteArray, and if so, how? I've had no luck doing so. If not, what's the best way to convert the entire array to quint16 array so that I can process the incoming data? Thanks All!!
QByteArray
is an array of bytes. To convert to a 16 bit unsigned integer value, you have to take two adjacent bytes from thebuffer
and usebuffer[n]*0xFF+buffer[n+1]
if the sender uses a big-endian system. Change the order if it is in little-endian system. – R Sahu