Reputation: 16341
I have the following code that I need to translate into qt from plain c++:
// txMessage and UserData are char arrays
memcpy(&txMessage[18], UserData, 8);
So my conversion is like this:
QByteArray txMessage;
txMessage.resize(26);
:
:
memcpy(&(txMessage.data()[18]), UserData, 8);
So this should work, but looks messy. I have to get the data pointer using the data() function and then reference element 18 with [18] and then take the address of that with &.
I looked through the whole QByteArray docs online and can't see another function that returns char* and operator [] only returns a temp variable, so can't address that. But there are so many options/functions for QByteArray maybe I missed something? Anyone found/got a better way to get the address of nth element in QByteArray?
EDIT There are a few things that appear to be undocumented in QByteArray:
Upvotes: 0
Views: 1328
Reputation: 37616
You should use the replace
method instead:
txMessage.replace(18, 8, UserData, 8) ;
If you are using a Qt version less than 4.7, but your UserData
variable is null-terminated, then simply:
txMessage.replace(18, 8, UserData) ;
Otherwize:
memcpy(txMessage.data() + 18, UserData, 8);
Upvotes: 2