Results 1 to 2 of 2

Thread: parsing messages with NUL ('\0') terminated tokens into QByteArray

  1. #1
    Join Date
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default parsing messages with NUL ('\0') terminated tokens into QByteArray

    Hi,
    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.

    Ex:
    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.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: parsing messages with NUL ('\0') terminated tokens into QByteArray

    you could read raw binary with QDataStream and parse that.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 8
    Last Post: 28th December 2009, 11:32
  2. Replies: 9
    Last Post: 25th July 2009, 13:27
  3. collect2: ld terminated with signal 9 [Killed]
    By mailtome in forum Installation and Deployment
    Replies: 2
    Last Post: 12th July 2008, 22:57
  4. Replies: 3
    Last Post: 2nd August 2007, 21:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.