Hello,

I am writing an application which has to receive UDP datagrams originated from an external source. I read the datagrams from the socket using QByteArray. The datagram has several fields, a 2-byte number, a 4-byte number and a textual message. I can read the textual message with the following code:

Qt Code:
  1. QByteArray buffer;
  2. // ...
  3. // read datagram from socket
  4. // ...
  5. QString message = QString::fromAscii(buffer.mid(6, 10));
To copy to clipboard, switch view to plain text mode 

But the parsing of the other fields of the datagram doesn't work. For example:

Qt Code:
  1. bool correct = true;
  2. quint16 cmd = buffer.mid(0, 2).toUShort(&correct);
  3. // correct is false after conversion
To copy to clipboard, switch view to plain text mode 

I assume I can't use QDataStream to get the individual fields, as the data is not being serialized to a QDataStream on the other end of the comunication. Could anyone point out what I am doing wrong? Is there maybe a better way to parse the different fields of a datagram in order to process them on the receiving end?

Any help would be much appreciated.

Thank you,
catto