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 bytes order
Forum Updated to NodeBB v4.3 + New Features

QDataStream bytes order

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 8.7k 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.
  • K Offline
    K Offline
    kyoti
    wrote on last edited by
    #1

    Hi,

    I am receiving a stream of bytes from the serial port, as a QByteArray
    this QByteArray is passed to a QDataStream and then the order of the bytes is changed to Little Indian

    QByteArray data;
    ....
    ....
    @quint8 parameter1;
    quint16 parameter2;
    quint16 parameter3;

    QDataStream stream(data);

    stream.setByteOrder(QDataStream::LittleEndian);
    
    stream.setVersion(QDataStream::Qt_4_5);
    
    stream >> parameter1;
    
    stream >> parameter2;
    
    stream >> parameter3;@
    

    if the data contained the following bytes
    byte1, byte2, byte3, byte4, byte5
    what is the order of the bytes after the little indian

    is it:
    byte5, byte4, byte3, byte2, byte1...?

    and then:
    parameter1 (byte5)
    parameter2(byte4, byte3)? OR (byte3, byte4)?
    parameter3(byte2, byte1)? OR (byte1, byte2)?

    I am sorry it just that I am a little bit confused??

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      No. The endianness only applies to the stored data itself, not the order in which you stored them. Qt will not magically reorder your stream reading. So, parameter 1 will be in byte 1, parameter 2 will occupy bytes 2 and 3, and parameter 3 bytes 4 and 5. The endianness only applies to the format of these (sets of) bytes.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kyoti
        wrote on last edited by
        #3

        but shouldn't it be:
        parameter1 (byte5)
        parameter2(byte4, byte3)
        parameter3(byte2, byte1)

        because it is .....Little indian??!!

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          No, it should not. Little Endian does not mean that the whole data structure is read back-to-front instead of the other way around.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kyoti
            wrote on last edited by
            #5

            so if I understood you right, then it depends on the received data structure, if it is
            already in the Little indian order (least significant byte to Most significant byte) then
            leave it as it is , and if not then read back-to-front

            .....? is that what you mean

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              No, no, no.

              To cite wikipedia:
              [quote]In computing, the term endian or endianness refers to the ordering of individually addressable sub-components within the representation of a larger data item as stored in external memory (or, sometimes, as sent on a serial connection).[/quote]

              That is, the endianness only applies to the internal ordering of the sub-components of the data representation. It does not apply to the ordering of these subcomponents within the data representation itself.

              The order of your data stream itself is not changed anywhere, or by anyone. It will stay as it is. If your data is written in the order parameter1, parameter2, parameter3, they will stay in that order and should be read back in that order, no matter what endianness was used in writing. The endianness only applies to the byte ordering within these parameters.

              Reflect a bit on what would happen if it applied to the whole data representation in the case of working with a large data stream (say, a HD video streaming service). Do you really think that in case a little-endian prepresentation would be used, the whole datastream needs to be transferred to the client before that client can start processing it? You think that the first frame of the video on that stream will be at the end of the stream? That would not make a very good streaming service, would it?

              1 Reply Last reply
              0

              • Login

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