Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDataStream pack and unpack
Forum Updated to NodeBB v4.3 + New Features

QDataStream pack and unpack

Scheduled Pinned Locked Moved Unsolved General and Desktop
qdatastream
5 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nickpp
    wrote on last edited by A Former User
    #1

    I am trying to write a function to pack/unpack data from/to qdatastream.
    void pack(QDataStream& myStream, quint64 data, int size) to copy size bits of data to myStream. Each bit of data can be stored as 1 byte of data in myStream.
    In similar way, I am trying to write another function to unpack data from stream
    quint64 unpack(QDataStream& myStream, int size) to unpack size bits data from myStream, and the return value is quint64.
    I can do it without funciton, but with function, I have no idea how to write these two.
    Any help?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      I can't make sense of the question, I'm sorry. could you give an example of what should get in those functions and what should be the result?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • N Offline
        N Offline
        nickpp
        wrote on last edited by
        #3

        main:

        QDataStream dataStream;
        pack(dataStream, 0x123, 12);
        The Stream should have these information: pack 0001 0010 0011 to the dataStream
        0000 0001 0000 0001 0000 0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 0000 0000 0001 0000 0000 0000 0000 0000 0000.
        1 byte of stream will represent 1 bit in data as this sequence: 1100 0100 1000 (LSB to MSB).
        When I unpack the dataStream by using funciton: unpack(dataStream, 4), the process will be in inverted order:
        assigned every byte from dataStream to a bit of data. The data taken out from stream and remaining stream will be:
        data: 0011 = 3
        0000 0001 -->1
        0000 0001 -->1
        0000 0000 -->0
        0000 0000 -->0
        remaining stream:0001 0010 = 12
        0000 0000 0000 0001 0000 0000 0000 0000 0000 0001 0000 0000 0000 0000 0000 0000.
        Note: when packing data, it will add the data to the right, and unpackData, the data will be removed from the left of the stream.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Oh  God Why

          Anyway,

          http://doc.qt.io/qt-5/qdatastream.html#writeRawData

          for(;data;data/=static_cast<quint64>(2)){
          const qint8 byte= data%2;
          myStream.writeRawData(&byte,1);
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          kshegunovK 1 Reply Last reply
          0
          • VRoninV VRonin

            Oh  God Why

            Anyway,

            http://doc.qt.io/qt-5/qdatastream.html#writeRawData

            for(;data;data/=static_cast<quint64>(2)){
            const qint8 byte= data%2;
            myStream.writeRawData(&byte,1);
            }
            
            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            @VRonin, division by 2, really? I know you can do much better ;)

            while (data)  {
                const quint8 byte = data & 1; // data % 2
                data >>= 1; // data / 2
                myStream.writeRawData(&byte, 1);
            }
            

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved