3

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.

mbuechmann
  • 5,413
  • 5
  • 27
  • 40
  • 2
    if you use this for transferring data between machines or saving to file shared by multiple machines. Then if both machines have different architecture this approach will not work properly (for example arm vs x86). See QDataStream. – Marek R Oct 28 '14 at 09:46

2 Answers2

4

You can cast it:

QByteArray arr;
mavlink_attitude_t* m = reinterpret_cast<mavlink_attitude_t*>(arr.data());
yandreiy
  • 181
  • 2
  • 10
  • 1
    reinterpret_cast should be avoided at all times. It doesn't always do what you think... I suggest to use a structured means of exchanging data, e.g. some messaging library like protobuf, cap'n proto or QDataStream. All of them are platform, machine and compiler(!) agnostic. This solution is platform, machine and compiler-dependent. – dom0 Oct 28 '14 at 11:42
  • 3
    @dom0: why should it be avoided "at all times"? While, it is generally not needed, it is sometimes very useful. Furthermore, not every application wants to be cross-platform. My commercial project is intentionally compiler, machine and platform agnostic for good. – László Papp Oct 28 '14 at 20:38
  • `QDataStream` can be used for serialisation and making data platform-independent. After sending over and receiving it on another platform, it is appropriate for this new platform. So I think this approach should works. – S.M.Mousavi Jan 10 '16 at 11:30
  • -1: .data() is null terminated '\0'. if bytearray.at(0) == '\0' (time_boot_ms=256 for example) , the first byte is '\0' then all the rest will be undetermined. – Phiber Feb 24 '17 at 15:24
  • Don't spread panic about reinterpret_cast. It is a dangerous, but extremely handy tool. It has to be used correctly, that's it. – Michał Leon Sep 16 '22 at 13:07
1

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.

Phiber
  • 1,041
  • 5
  • 17
  • 40