Reputation:
I need to convert a QByteArray
to structure.
I have a structure like this:
struct mavlink_attitude_t
{
/// <summary> Timestamp (milliseconds since system boot) </summary>
quint32 time_boot_ms;
/// <summary> Roll angle (rad, -pi..+pi) </summary>
float roll;
/// <summary> Pitch angle (rad, -pi..+pi) </summary>
float pitch;
/// <summary> Yaw angle (rad, -pi..+pi) </summary>
float yaw;
/// <summary> Roll angular speed (rad/s) </summary>
float rollspeed;
/// <summary> Pitch angular speed (rad/s) </summary>
float pitchspeed;
/// <summary> Yaw angular speed (rad/s) </summary>
float yawspeed;
};
and I have a QbyteArray
comes from the serial port.
I already used union but I think it can't be used for QByteArray
.
Is there any other way? an example can really help.
Upvotes: 3
Views: 9943
Reputation: 1103
The accepted response could not work. .data()
is null terminated.
You have floats and ints, use shift left.
Example:
mavlink_attitude_t.time_boot_ms = (bytearray.at(0) & 0xFFFF) |
(bytearray.at(1) & 0xFFFF) << 8 |
(bytearray.at(2) & 0xFFFF) << 16|
(bytearray.at(3) & 0xFFFF) << 24
if you use Little endian, inverse the indexs.
Upvotes: 1
Reputation: 191
You can cast it:
QByteArray arr;
mavlink_attitude_t* m = reinterpret_cast<mavlink_attitude_t*>(arr.data());
Upvotes: 4