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. [Solved]where does the extra bytes come from?
Forum Updated to NodeBB v4.3 + New Features

[Solved]where does the extra bytes come from?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 4.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.
  • S Offline
    S Offline
    shalongbasi
    wrote on last edited by
    #1

    with the following codes, when monitored by WireShark, there are extra 4 bytes 0,0,0,0x26(&) coming before my string "aaabbbccc..." in the UDP Packet data part. where are these bytes coming from? thanks.

    @{
    QByteArray datagram;
    QDataStream out(&datagram,QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_8);
    out << QByteArray("aaabbbcccdddeeefff");
    udpSocket.writeDatagram(datagram,QHostAddress("10.10.10.255",49166);
    }@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      When you use a QDataStream, there is metadata which is included with the data that is streamed; the bytes streamed are not just the contents of the data items themselves. In the case of a QByteArray, the data is preceded by a quint32 which contains the count of the bytes which follow.

      See "here":/doc/qt-4.8/datastreamformat.html for more specific information.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        shalongbasi
        wrote on last edited by
        #3

        Thanks. But when I try to output a float number, it always occupy 8 bytes, not 4 bytes
        the last two items is the value from QDoubleSpinBox, it should be double, but I forced it to float, but it still output 64bits. ???
        @
        out.setVersion(QDataStream::Qt_4_8);
        out << baseTime << opMode << waveform << (quint8) ui->PowerspinBox->value()
        << (quint16) ui->VoltageSetBox->value() << (float) ui->CurrentSetBox->value()
        << (float) ui->FREQSetBox->value();
        @

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Code_ReaQtor
          wrote on last edited by
          #4

          I am not sure if this will work, try to set the "FloatingPointPrecision":http://qt-project.org/doc/qt-4.8/qdatastream.html#FloatingPointPrecision-enum
          The doc says about 32-bit precision (4 bytes) and 64-bit precision (8 bytes)

          or try casting it like this @(float32) ui->FREQSetBox->value();@

          Note: not tested on actual codes

          Please visit my open-source projects at https://github.com/Code-ReaQtor.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            If you want byte-for-byte control over what is going on the wire then use QIODevice::write() directly and worry about packing/unpacking, byte-ordering-issues and platform differences for yourself.

            QDataStream is a platform independent serialisation mechanism designed to facilitate communication between two QDataStream endpoints. The C++ standard does not mandate the actual size of float or double (just that double must be at least as big as float) so, in order to be consistent between platforms/compiler, the QDataStream code outputs all floating point numbers in a 64-bit IEEE format. That usually corresponds to the double type for desktop machines but, IIRC, some mobile platforms use 32-bit floating point for both types. You can force QDataStream to use 32-bit floating point instead but then all floating point is reduced to 32bits.

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

              You can set the floating point precision that QDataStream is supposed to use:
              [quote]void QDataStream::setFloatingPointPrecision(FloatingPointPrecision precision)
              Sets the floating point precision of the data stream to precision. If the floating point precision is DoublePrecision and the version of the data stream is Qt_4_6 or higher, all floating point numbers will be written and read with 64-bit precision. If the floating point precision is SinglePrecision and the version is Qt_4_6 or higher, all floating point numbers will be written and read with 32-bit precision.[/quote]

              Also, when writing a QByteArray, there must be extra bytes written for a counter that precedes the actual data. That's because otherwise the corresponding read operation couldn't know how many bytes follow...

              My OpenSource software at: http://muldersoft.com/

              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

              Go visit the coop: http://youtu.be/Jay...

              1 Reply Last reply
              0
              • S Offline
                S Offline
                shalongbasi
                wrote on last edited by
                #7

                thanks for everyone's help, QDataStream::setFloatingPointPrecision(QDataStream::SinglePrecision) fixed the problem. The other side is a micontroller, not running Qt, so I have to handle the data byte by byte.

                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