[Merged] copying data from structure to a ByteArray and vise versa
- 
Have You tried QDataStream with a QBuffer. 
 @
 struct YourStruct
 {
 quint8 x;
 quint16 y;
 float z;YourStruct() :x(0),y(0),z(0.0f) {} YourStruct(const YourStruct& other) :x(other.x),y(other.y),z(other.z) {} bool operator == (const WeaponStats& other) const { ... } bool operator < (const WeaponStats& other) const { ... } bool operator > (const WeaponStats& other) const { ... }}; inline QDataStream& operator<<(QDataStream& out, const YourStruct& str) 
 {
 out << str.x;
 out << str.y;
 out << str.z;
 return out;
 }inline QDataStream& operator>>(QDataStream& in, YourStruct& str) 
 {
 in >> str.x;
 in >> str.y;
 in >> str.z;
 return in;
 }Q_DECLARE_METATYPE(YourStruct) //before using call 
 qRegisterMetaTypeStreamOperators<YourStructs>("YourStruct");
 @or just have a look "here":http://www.qtcentre.org/wiki/index.php?title=Using_custom_data_types_with_Qt#QDataStream 
- 
Post Irrelevant since Merged 
- 
char * QByteArray::data () 
 Returns a pointer to the data stored in the byte array.
 Have You tried this.
- 
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;
 TX_Buffer.resize(2049);
 QByteArray RX_Buffer;
 RX_Buffer.resize(2049);
 bool ok;memset(&TX_Msg.WakeUp[0], 0xFE, sizeof(TX_Msg.WakeUp)); TX_Msg.Header = 0x68; TX_Msg.Address[0] = 0x09; TX_Msg.Address[1] = 0x90; TX_Msg.Address[2] = 0x78; TX_Msg.Address[3] = 0x56; TX_Msg.Address[4] = 0x34; TX_Msg.Address[5] = 0x12; TX_Msg.Delimeter = 0x68; TX_Msg.R_W = 0x01; TX_Msg.DataLength = 0x02; memmove((void *)TX_Buffer.data(), &TX_Msg, sizeof(TX_Msg));@ 
- 
Do you need memmove for speedup or for something else? In first case I think using QByteArray will remove all advantages of using memmove. 
- 
Denis: Why? You can reserve a chunk of memory and qMemCopy into it. Why is that slower as using some other method? 
- 
phamtv: I'd recommend using qMemCopy/qMemSet from QtCore/QtGlobal. I think those are not documented, so they are not officially supported, but they work well for me. 
- 
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":http://doc.trolltech.com/4.7/qbytearray.html, 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. 
- 
That time tests: @ QByteArray ba1(2048, 0); 
 QByteArray ba2(2048, 255);
 QByteArray ba3(1024, 255);
 char *ba1data = ba1.data();
 char *ba2data = ba2.data();QElapsedTimer timer; 
 timer.start();for (int i = 0; i < 100000000; i++) 
 {
 //ba2.replace(1000, 1024, ba1.data()+1000, 1024); // For 12324 ms.
 //ba2.replace(1000, 1024, ba1.mid(1000, 1024)); // For 33228 ms.
 //ba3 = ba1.left(1024); // For 45615 ms. <- ?!
 //ba3 = ba1.right(1024); // For 29640 ms.
 //ba3 = ba1.mid(1000, 1024); // For 29859 ms.//memcpy(ba2data+1000, ba1data+1000, 1024); // For 7722 ms. qMemCopy(ba2data+1000, ba1data+1000, 1024); // For 11981 ms.} qDebug() << timer.elapsed();@ 
