View Full Version : parsing messages with NUL ('\0') terminated tokens into QByteArray

19th January 2010, 00:33
I'm trying to code a TCP socket interface who's protocol uses messages with fields that are all '\0' terminated. The messages do not have a deliminator except the '\0' after the last field.

msgId1\0msgVersion1\0msgField1\0msgField2msgID\0ms gId2\0msgVersion2\0msgField1\0msgField2\0msgField3 .... etc..

The problem is I'm trying to read this data out using a QTextStream into a ByteArray but I appear to have problems with the null terminated characters because QTextStream doesn't want to read beyond the first \0 and when I output the token I don't even get the ""'s that are shown when you qDebug() print the QString.

Writing data out is easy as I can do QTextStream << msgID << '\0' << msgVersion << \0 << etc....

Is there a simple way to do this with QT? Currently I'm just reading one byte at a time from my QTcpSocket and creating a new token when I hit the \0 character. Matching messages is by msgID and version so I have to run the tokens serially into a function that reports a complete message received when the correct # of tokens has been received per the msgID and msgVersion.

Just curious if someone has a simple means of parsing data like this and recognizing messages using some type of pattern in QT.


20th January 2010, 10:01
you could read raw binary with QDataStream and parse that.