Best way to create and store binary data
-
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.
-
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.
-
@GregWilsonLindberg QByteArray will resize automatically. resize() and reserve() are there to improve performance (avoid multiple reallocs).
-
@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.
-
@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.
-
@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). -
@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