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.
You can cast it:
QByteArray arr;
mavlink_attitude_t* m = reinterpret_cast<mavlink_attitude_t*>(arr.data());
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.