Results 1 to 6 of 6

Thread: Struct to QByteArray

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Struct to QByteArray

    Hello

    Based on this topic, I would like to know:

    Is there another reasonable way of converting a struct to a QByteArray that doesn't use QDataStream?

    Thanks,

    Momergil

    Edit: the only other way I'm aware now is:

    Qt Code:
    1. MyStruct structTemp;
    2.  
    3. //Fill structTemp
    4.  
    5. char buffTemp[sizeof(structTemp)];
    6.  
    7. memcpy(buffTemp, &structTemp, sizeof(structTemp));
    8.  
    9. QByteArray baTemp = QByteArray::fromRawData(buffTemp,sizeof(structTemp)));
    To copy to clipboard, switch view to plain text mode 
    Last edited by Momergil; 3rd July 2014 at 18:07.
    May the Lord be with you. Always.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,418
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Struct to QByteArray

    Well, this is not a reasonable way

    You can basically use any form of serialization that has a bytestream at the end.

    E.g. JSON and then converting the string to the byte array, or XML, or any other text based format.

    Or defining your own binary format, etc.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Struct to QByteArray

    Quote Originally Posted by anda_skoa View Post
    Well, this is not a reasonable way
    \o/ Why? =D it does look fast, clear and easy for me

    Now JSON or XML? o.O I mean, how does converting a struct into a string inside a file and then reading that file to QByteArray is anyway reasonable?

    About the binary solution, I'vr no idea on how to do that xD


    Thanks,

    Momergil
    May the Lord be with you. Always.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,418
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Struct to QByteArray

    Quote Originally Posted by Momergil View Post
    \o/ Why? =D it does look fast, clear and easy for me
    Usually serialization is used to transport data from one application to another, sometimes even across machine boundaries.

    Copying the runtime memory representation instead means that it can only be read by a prgram written in exactly the same language, using the same compiler and the build using the same options, running on the same machine.

    And putting external data unchecked into memory has the security implications.

    Quote Originally Posted by Momergil View Post
    Now JSON or XML? o.O I mean, how does converting a struct into a string inside a file and then reading that file to QByteArray is anyway reasonable?
    You don't have to write to a file, both QJsonDocument and QXmlStreamWriter/-Reader can work with in-memory destination/sources, including QByteArray.

    Quote Originally Posted by Momergil View Post
    About the binary solution, I'vr no idea on how to do that xD
    QDataStream for example if the communication is between two Qt programs.
    Or Google protocol buffers or a hand-crafted format.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Momergil (4th July 2014)

  6. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Struct to QByteArray

    Thanks once again anda_skoa for you reply!


    Quote Originally Posted by anda_skoa View Post
    Copying the runtime memory representation instead means that it can only be read by a prgram written in exactly the same language, using the same compiler and the build using the same options, running on the same machine.
    Well I certainly don't understand how does that apply to the code I wrote above! o.O (I mean, if you are talking about the code and not about the whole concept of struct-to-array). I just can't see why it would have much of these limitations you mentioned with the exception of the language one, since I agree that the whole thing presupposes that the reading guy will have the header containing that struct specification (which means a C or C++ app). But what does compiler has to do with it? And even more be in the same machine? o.O
    May the Lord be with you. Always.

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Struct to QByteArray

    For example, the compiler is free to insert padding bytes to ensure data items within the structure align on convenient boundaries for the machine architecture you are targeting. Different compilers may do this in different ways (or not at all) even on the same machine. A compiler may chose different sizes for the basic types: the standard only requires, for example, that an int is at least as big as a short which is in turn at least as big as a char. One compiler's internal representation for a given class/struct is therefore not reliably the same as another.

    Taking the structure between architectures also exposes you to byte order issues even if the compilers agree on exactly how big data elements are and padding is done. A little-endian 4 byte int is not sensible if interpreted as a 4-byte big-endian int.

    These issues are precisely why we have mechanisms like QDataStream, or lower functions like htons() or ntohs(), to provide a transportation format that is independent of the internal storage form.

  8. The following user says thank you to ChrisW67 for this useful post:

    Momergil (7th July 2014)

Similar Threads

  1. QList<struct>
    By Axsis in forum Newbie
    Replies: 11
    Last Post: 12th October 2015, 07:48
  2. Replies: 6
    Last Post: 14th May 2014, 12:14
  3. Replies: 11
    Last Post: 3rd April 2012, 04:51
  4. Replies: 1
    Last Post: 22nd June 2011, 08:12
  5. Replies: 9
    Last Post: 25th July 2009, 13:27

Tags for this Thread

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.