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. Best way to create and store binary data
QtWS25 Last Chance

Best way to create and store binary data

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 2.5k Views
  • 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.
  • G Offline
    G Offline
    GregWilsonLindberg
    wrote on last edited by
    #1

    I'm building an app to parse an elf file (executable) and read in the .text and .data sections to pass on to a microprocessor for updating.
    It seems as if the QByteArray is the best way store the data as I'm processing the elf file. My question then becomes one of getting the data from the elf file into the QByteArray. The append() function seems to be the only method to add data at the end of the array, but it is unclear from the documentation if append(const char *str, int len) will properly append data that may contain zeros if the length is larger than the distance to the zero byte. i.e. I want to append 4K bytes to the QByteArray but there is a string of 5 zero bytes 2300 bytes into the 4K byte block, will append copy the complete 4K block?

    Also, what is the best way to make sure that the QByteArray has enough memory in it, resize() or reserve(). I will just be adding a few blocks to the end of the first copy into it, maybe 3 or 4?

    But maybe I should just use malloc() to allocate an array and then use memcpy() to copy data into it.

    jsulmJ 1 Reply Last reply
    0
    • O Offline
      O Offline
      Oleksandr Malyushytskyy
      wrote on last edited by
      #2

      You can use QDataStream to write to QByteArray, (as well as read data from files) look at constructors:
      QDataStream(QByteArray * a, QIODevice::OpenMode mode)

      Keep in mind that QByteArray is limited size of int.

      G 1 Reply Last reply
      2
      • G GregWilsonLindberg

        I'm building an app to parse an elf file (executable) and read in the .text and .data sections to pass on to a microprocessor for updating.
        It seems as if the QByteArray is the best way store the data as I'm processing the elf file. My question then becomes one of getting the data from the elf file into the QByteArray. The append() function seems to be the only method to add data at the end of the array, but it is unclear from the documentation if append(const char *str, int len) will properly append data that may contain zeros if the length is larger than the distance to the zero byte. i.e. I want to append 4K bytes to the QByteArray but there is a string of 5 zero bytes 2300 bytes into the 4K byte block, will append copy the complete 4K block?

        Also, what is the best way to make sure that the QByteArray has enough memory in it, resize() or reserve(). I will just be adding a few blocks to the end of the first copy into it, maybe 3 or 4?

        But maybe I should just use malloc() to allocate an array and then use memcpy() to copy data into it.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @GregWilsonLindberg QByteArray will resize automatically. resize() and reserve() are there to improve performance (avoid multiple reallocs).

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        G 1 Reply Last reply
        1
        • O Oleksandr Malyushytskyy

          You can use QDataStream to write to QByteArray, (as well as read data from files) look at constructors:
          QDataStream(QByteArray * a, QIODevice::OpenMode mode)

          Keep in mind that QByteArray is limited size of int.

          G Offline
          G Offline
          GregWilsonLindberg
          wrote on last edited by GregWilsonLindberg
          #4

          @Oleksandr-Malyushytskyy, I'm not exactly sure what the QDataStream buys me, I'm just trying to copy data from one place in memory to another, the input file needs to be processed by libelf to get to the actual .text and .data blocks, and the output data will be send along a CAN bus using CanFestival. I just need the QByteArray to allow marshalling the data together so it can be sent out on the CAN bus.

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @GregWilsonLindberg QByteArray will resize automatically. resize() and reserve() are there to improve performance (avoid multiple reallocs).

            G Offline
            G Offline
            GregWilsonLindberg
            wrote on last edited by GregWilsonLindberg
            #5

            @jsulm, From parsing the elf file I know what the total size of all of the data will be, so pre-allocation of the QByteArray to avoid reallocs seems like the best policy. I was just wondering if one of them was preferable to the other.

            I still don't know if the append(const char *. int) call will properly handle data with embedded null bytes.

            jsulmJ kshegunovK 2 Replies Last reply
            0
            • G GregWilsonLindberg

              @jsulm, From parsing the elf file I know what the total size of all of the data will be, so pre-allocation of the QByteArray to avoid reallocs seems like the best policy. I was just wondering if one of them was preferable to the other.

              I still don't know if the append(const char *. int) call will properly handle data with embedded null bytes.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @GregWilsonLindberg I think in your case resize() is the method to use.
              If you want to handle data containing zero bytes you can use http://doc.qt.io/qt-5/qbytearray.html#fromRawData, append() interprets the pointer as a C string as far as I know, so zero bytes will be interpreted as end of string (not what you want).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • G GregWilsonLindberg

                @jsulm, From parsing the elf file I know what the total size of all of the data will be, so pre-allocation of the QByteArray to avoid reallocs seems like the best policy. I was just wondering if one of them was preferable to the other.

                I still don't know if the append(const char *. int) call will properly handle data with embedded null bytes.

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

                @GregWilsonLindberg said in Best way to create and store binary data:

                I still don't know if the append(const char *. int) call will properly handle data with embedded null bytes.

                It will if you pass a valid size.

                @jsulm said in Best way to create and store binary data:

                append() interprets the pointer as a C string as far as I know, so zero bytes will be interpreted as end of string (not what you want).

                Only if you don't pass the size (i.e. cause it to deduce the size of the buffer).

                http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qbytearray.cpp#n1922

                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