[Solved]where does the extra bytes come from?

  • 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 << QByteArray("aaabbbcccdddeeefff");

  • Moderators

    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.

  • 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 << baseTime << opMode << waveform << (quint8) ui->PowerspinBox->value()
    << (quint16) ui->VoltageSetBox->value() << (float) ui->CurrentSetBox->value()
    << (float) ui->FREQSetBox->value();

  • 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

  • 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.

  • 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...

  • 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.