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. Appending quint16 to QByteArray
Forum Updated to NodeBB v4.3 + New Features

Appending quint16 to QByteArray

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 4.4k 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.
  • M Offline
    M Offline
    mulfycrowh
    wrote on last edited by
    #1

    Hi,

    I would like to append a quint16 variable to a QByteArray to write to a file.
    A quint16 is made of 2 bytes, right ?
    So I wrote:

    QByteArray qbatow;
    ...
    quint16 towrite = 256;
    qbatow = qbatow.append(towrite);
    

    The trouble is in the file: I always get 1 byte only in the file instead 2 bytes. I get the LSB, that's to say 00.

    Any idea ?

    Thanks

    kshegunovK 1 Reply Last reply
    0
    • M mulfycrowh

      Hi,

      I would like to append a quint16 variable to a QByteArray to write to a file.
      A quint16 is made of 2 bytes, right ?
      So I wrote:

      QByteArray qbatow;
      ...
      quint16 towrite = 256;
      qbatow = qbatow.append(towrite);
      

      The trouble is in the file: I always get 1 byte only in the file instead 2 bytes. I get the LSB, that's to say 00.

      Any idea ?

      Thanks

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @mulfycrowh

      QByteArray qbatow;
      
      QDataStream out(&qbatow, QIODevice::WriteOnly | QIODevice::Append);
      out.setByteOrder(QDataStream::BigEndian);
      
      quint16 towrite = 256;
      out << towrite;
      

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mulfycrowh
        wrote on last edited by
        #3

        No ! I don't want to use the operator <<. I use writeRawData

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mulfycrowh
          wrote on last edited by
          #4

          I found the solution :

          	quint16 towrite = 256;
          	char* charToWrite = (char*)&towrite;
          	qbatow = qbatow.append(charToWrite, sizeof(quint16));
          
          	out.writeRawData(qbatow.data(), qbatow.length());
          

          It's OK in the file : 00 01

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on last edited by
            #5

            It is better to use '<<' operator with QDataStream. Just configure QDataStream with desired BigEndian/LowEndian.

            1 Reply Last reply
            1
            • M Offline
              M Offline
              mulfycrowh
              wrote on last edited by
              #6

              No it is not better. First of all, I used << and I didn't get the right count of bytes because depending of serialization. It's better to use writeRawData for my purpose.

              kshegunovK 1 Reply Last reply
              0
              • M mulfycrowh

                No it is not better. First of all, I used << and I didn't get the right count of bytes because depending of serialization. It's better to use writeRawData for my purpose.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #7

                @mulfycrowh

                No it is not better.

                Yes, it is, you just don't realize it yet. Run your program on Intel and on PowerPC and compare the data you get in the byte array.

                First of all, I used << and I didn't get the right count of bytes because depending of serialization.

                Then you did something wrong.

                It's better to use writeRawData for my purpose.

                I don't know what exactly is your purpose, but I really, really doubt writeRawData is better.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                1

                • Login

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