Results 1 to 10 of 10

Thread: QDataStream >>QByteArray [was QByteArray resize not working?!]

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QDataStream >>QByteArray [was QByteArray resize not working?!]

    Hi

    I have this piece of code:

    Qt Code:
    1. records >> sPacket.size_app_payload; // == 15
    2. sPacket.app_payload.resize( sPacket.size_app_payload );
    3.  
    4. printf("Application Payload array size: %d\r\n", sPacket.app_payload.size() );
    To copy to clipboard, switch view to plain text mode 

    the
    Qt Code:
    1. sPacket.size_app_payload
    To copy to clipboard, switch view to plain text mode 
    returns 15

    the
    Qt Code:
    1. sPacket.app_payload.size()
    To copy to clipboard, switch view to plain text mode 
    returns 0 !!!

    Unless I'm finally going mad or short of spotting something very obvious this is weird!

    TIA,
    Pedro.
    Last edited by pdoria; 24th July 2009 at 16:42.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QByteArray resize not working?!

    Qt Code:
    1. QByteArray ba("qtcentre.org");
    2. qWarning() << ba.size(); // 12
    3. ba.resize(4);
    4. qWarning() << ba.size(); // 4
    To copy to clipboard, switch view to plain text mode 

    works just fine, so what type is your sPacket? and what is the output of

    Qt Code:
    1. records >> sPacket.size_app_payload;
    2. qWarning() << sPacket.size_app_payload << sPacket.app_payload.size();
    3. sPacket.app_payload.resize( sPacket.size_app_payload );
    4. qWarning() << sPacket.size_app_payload << sPacket.app_payload.size();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QByteArray resize not working?!

    Hi Lykurg. Txs for replying

    I've somewhat pinpointed the problem...
    The resize actually works

    The problem is when I throw data into the byte array as exemplified:

    Qt Code:
    1. records >> sPacket.size_app_payload;
    2. printf ("size_app_payload: %d\r\n", sPacket.size_app_payload);
    3. sPacket.app_payload.resize( sPacket.size_app_payload );
    4. printf ("app_payload size after resize(): %d\r\n",sPacket.app_payload.size() );
    5. records >> sPacket.app_payload; //! problem line!
    6. printf ("app_payload size after records >> : %d\r\n",sPacket.app_payload.size() );
    To copy to clipboard, switch view to plain text mode 

    this is the output:
    size_app_payload: 15
    app_payload size after resize(): 15
    app_payload size after records >> : 0

    the sPacket is of:
    Qt Code:
    1. typedef struct {
    2. quint8 DLE; // ASCII DLE character (16 decimal)
    3. quint8 packet_id; // packet ID
    4. // types:
    5. // 6 - ACK
    6. // 10 - Command
    7. // 14 - Date/Time Data
    8. // 21 - NAK
    9. // 38 - Unit ID/ESN
    10. // 51 - PVT (Position, Velocity, Time) Data
    11. // 135 - Legacy Stop message
    12. // 136 - Legacy text message
    13. // 161 - Fleet Management packet
    14. quint8 size_app_payload; // number of bytes of packet data (bytes 3 to n-4)
    15. QByteArray app_payload; // 0 to 255 bytes
    16. quint8 checksum; // 2's complement of the sum of all bytes from byte 1 to byte n-4 (end of the payload)
    17. quint8 DLE_end; // same as DLE
    18. quint8 ETX; // End of text - ASCII ETX character (3 decimal)
    19. } serial_packet_format;
    To copy to clipboard, switch view to plain text mode 

    and I know there's data pending in the datastream ...

    So... the above code compiles without a problem... I'm wondering if there's a problem with
    Qt Code:
    1. records >> sPacket.app_payload;
    To copy to clipboard, switch view to plain text mode 
    this syntax ...

    TIA,
    Pedro.

  4. #4
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream >>QByteArray [was QByteArray resize not working?!]

    bump
    anyone?

  5. #5
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream >>QByteArray [was QByteArray resize not working?!]

    Gurus out there...

    I know I can solve it with

    Qt Code:
    1. qint8 c;
    2. for (int i = 0; i < sPacket.size_app_payload; i++) {
    3. records >> c;
    4. sPacket.app_payload[i] = c;
    5. }
    To copy to clipboard, switch view to plain text mode 

    ... but that's rather inefficient, right?

    Given the fact that the QByteArray sPacket.app_payload has already been re-sized one assumes that the >> operator fetches sPacket.app_payload.size() bytes, right?

    So why does
    Qt Code:
    1. records >> sPacket.app_payload;
    To copy to clipboard, switch view to plain text mode 
    call fail?

    TIA,
    Pedro

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream >>QByteArray [was QByteArray resize not working?!]

    Does the data sitting in the stream actually represent a byte array? How was it written to the stream?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream >>QByteArray [was QByteArray resize not working?!]

    Hi wysota. txs for taking an interest!

    The datastream has been filled by data coming from a socket so, yes, it's conceptually a byte array.

    As a sample, from inspecting it byte-by-byte:

    11 |
    8 | Driver ID update 0x0811 (quint16)
    1 |
    0 |
    0 |
    0 | status_change_id (quint32)
    bd |
    25 |
    bb |
    24 | time_type (quint32)
    4d | "M"
    47 | "G"
    50 | "P"
    53 | driver_id "S" (variable length, null terminated, string)
    0 | null-termination
    3e | checksum
    10 | DLE
    3 | ETX
    All multi-byte numeric values represented here come in little-endian format which are (will be) dealt with by means of qToBigEndian.

    I'm also having trouble extracting values from the QByteArray .. like:
    I want to extract the first two bytes from it to a quint16 and then perform qToBigEndian on it. I'm also miserably failing to do that ...

    TIA,
    Pedro.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream >>QByteArray [was QByteArray resize not working?!]

    Quote Originally Posted by pdoria View Post
    The datastream has been filled by data coming from a socket so, yes, it's conceptually a byte array.
    Is it an array of bytes or a byte array object?
    Because if you are expecting QDataStream to build a QByteArray object from an arbitrary binary content, this will obviously not work. The same as it is not possible to ask QColor for hardware address of your network card.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream >>QByteArray [was QByteArray resize not working?!]

    Well ... it's an array of bytes of arbitrary binary content...

    funny thing the compiler is perfectly happy with QDataStream >> QByteArray...
    Why does it fail the call is still beyond me... (and this is exactly the kind of thing that gets you stuck for hours on end )

    To be honest, and never forgetting my place (newbie), I'd call this one a bug on QT4.
    This call should fetch myQByteArray.size bytes from the stream, regardless of content.
    Reading the docs for QByteArray it's stated "QByteArray can be used to store both raw bytes..." so it shouldn't matter what kind of content, or where it came from, one puts in it...


    Bottom line: is there no other (optimized) way than being stuck with this?

    Qt Code:
    1. qint8 c;
    2. for (int i = 0; i < sPacket.size_app_payload; i++) {
    3. records >> c;
    4. sPacket.app_payload[i] = c;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Regarding my other difficulty ...
    I want to extract the first two bytes from it to a quint16

    Could you please show me a way?

    TIA,
    Pedro.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream >>QByteArray [was QByteArray resize not working?!]

    Quote Originally Posted by pdoria View Post
    Well ... it's an array of bytes of arbitrary binary content...
    So it won't work.

    funny thing the compiler is perfectly happy with QDataStream >> QByteArray...
    Because this is syntactically correct.

    Why does it fail the call is still beyond me... (and this is exactly the kind of thing that gets you stuck for hours on end )
    Then read the first sentence of QDataStream documentation and then use QIODevice::read() on the socket directly.

    To be honest, and never forgetting my place (newbie), I'd call this one a bug on QT4.
    It's not a bug, it's a misunderstanding of what QDataStream is.

    This call should fetch myQByteArray.size bytes from the stream, regardless of content.
    No. Make the following experiment:
    Qt Code:
    1. QFile f("test.bin");
    2. f.open(QFile::WriteOnly);
    3. QDataStream dstream(&f);
    4. dstream << 7;
    5. f.close();
    To copy to clipboard, switch view to plain text mode 
    Now go and take a look at size of the created file and its content and everything will become clear.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    pdoria (25th July 2009)

Similar Threads

  1. resize of QMessageBox not working?
    By Sheng in forum Qt Programming
    Replies: 2
    Last Post: 13th November 2008, 14:25
  2. Replies: 2
    Last Post: 22nd January 2008, 16:10
  3. Custom Shape Widget (resize)
    By PiXeL16 in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2007, 07:00
  4. QDomElement to QByteArray ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 2nd May 2006, 17:01
  5. postponing resize event
    By Honestmath in forum Qt Programming
    Replies: 11
    Last Post: 26th February 2006, 00:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.