Qt World Summit: Register Today!

u_char* to QDataStream and readDouble() in the stream

  • Hello

    I'm trying to parse TCP Packets that comes in the form of u_char*

    But I haven't found a way to do it

    What I'm looking for is the same behavior as stream.readDouble() in c# so that I can parse different values from the packets without iterating through the u_char*

    To be more specific I'll give an example of the data I want to parse

    This is the packet with like multiple values in it [int][utf text][double][u_short]

    And I want something like

    int i = stream.readInt();
    char *text = stream.readUTF8();
    double number = stream.readDouble();
    u_short tinyNumber = stream.readUShort();

    Is this possible?

  • Lifetime Qt Champion

    @zwokaros Take a look at http://doc.qt.io/qt-5/qdatastream.html
    What type is stream?
    Assuming stream is QDataStream:

    int i;
    QString text;
    double number;
    u_short tinyNumber;
    stream >> i >> text >> number >> tinyNumber;

  • Lifetime Qt Champion


    Out of curiosity, where are these packets coming from ?

  • You can use @jsulm solution for all the types* apart for the text. You need to tell us how you can determine when the text finishes. Is it null terminated? does it have a header that tells you the size?

    * You should be careful with endianness but since you are saying it comes from TCP it's fair to assume the usual Big-Endian convention

Log in to reply