[Merged] copying data from structure to a ByteArray and vise versa
Can someone provide me and example on how to get this to work?
Have You tried QDataStream with a QBuffer.
- struct YourStruct
- quint8 x;
- quint16 y;
- float z;
- YourStruct(const YourStruct& other)
- bool operator == (const WeaponStats& other) const
- bool operator < (const WeaponStats& other) const
- bool operator > (const WeaponStats& other) const
- out << str.x;
- out << str.y;
- out << str.z;
- return out;
- in >> str.x;
- in >> str.y;
- in >> str.z;
- return in;
- //before using call
or just have a look here [qtcentre.org]
yes I have… the following code I have does not seem to work. I checked under the memory window after the memmove function but do not see the result I am expecting.
- struct_MtrTxMsgType TX_Msg;
- int txcnt, rcvLen;
- QByteArray TX_Buffer;
- QByteArray RX_Buffer;
- bool ok;
- memset(&TX_Msg.WakeUp, 0xFE, sizeof(TX_Msg.WakeUp));
- TX_Msg.Header = 0x68;
- TX_Msg.Address = 0x09;
- TX_Msg.Address = 0x90;
- TX_Msg.Address = 0x78;
- TX_Msg.Address = 0x56;
- TX_Msg.Address = 0x34;
- TX_Msg.Address = 0x12;
- TX_Msg.Delimeter = 0x68;
- TX_Msg.R_W = 0x01;
- TX_Msg.DataLength = 0x02;
- memmove((void *)TX_Buffer.data(), &TX_Msg, sizeof(TX_Msg));
Tobias, hm, I mostly saying that from my point of view memmove and other c things should be used now only if you are know what you are doing (e.g. if you need to speed up your memory management). In common cases such low-level work is not needed. And in case of mixing memmove for initializing data and using QByteArray to work with it I think initial speedup from memmove will be neglected by QByteArray abstractions.
Denis: You are right that you should not need these low level functionality 95% of the time… but it is still very useful for the last 5%:-)
I actually do not think that the QByteArray overhead is that big by the way (I did not do any measurements though), it is just a wrapper around a piece of memory after all. Resizing the buffer is of course expensive and needs to be avoided.
phamtv: You did read the documentation of QByteArray [doc.trolltech.com], didn’t you? It states quite clearly how to access its buffer.
yes I have read the QByteArray document. I am still puzzled on the benefits of a QByteArray. My background with byte arrays have always been something similar to quint8 arr in which I can later cast to whatever my data structure is. Besides dynamic resizing of the array size, are there any other advantages to QByteArray over the quint8? Also, is it true that if I use the QByteArray, I should use the qMemCopy/qMemSet from QtCore/QtGlobal instead of the memmove, memcpy???
To quote the documentation:
QByteArray can be used to store both raw bytes (including ‘\0’s) and traditional 8-bit ‘\0’-terminated strings. Using QByteArray is much more convenient than using const char *. Behind the scenes, it always ensures that the data is followed by a ‘\0’ terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.
Using QByteArray or not does not matter for qMemCopy or memcpy. The first is just a wrapper around the latter anyway. qMemCopy will be there wherever Qt is available, and it is in a Qt header which is included in almost all Qt code.