I have enum-type data like:
typedef enum
{
NULLTYPE = 0U,
ParameterGetType = 1U,
ParameterSetType = 2U,
ParameterStatusType = 3U,
EventActionType = 4U,
CommandType = 5U,
CommandGetType = 6U,
CommandSetType = 7U,
EquipmentIDGetType = 15U,
EquipmentIDSetType = 16U,
EquipmentIDStatusType = 17U,
EventReportGetType = 18U,
EventReportSetType = 19U,
EventReportStatusType = 20U,
PeriodicReportType = 21U,
PeriodicReportGetType = 22U,
PeriodicReportSetType = 23U,
PeriodicReportStatusType = 24U,
CommandResponseType = 25U,
CommandResponseGetType = 26U,
CommandResponseSetType = 27U,
CommandResponseStatusType = 28U,
CommandResponseDeletedType = 29U
}MessageType;
I want to save it into binary file using QByteArray and QFile::write(QByteArray),
but I don’t know the length of the data, and the ‘distribution’ of bytes’ value of the data,
(if the data is 1, will other bytes’ value be zero?)
I think it is not limited to qt, how to write the data into file?
(For short-type data (MsgItem->PIN), I will do this:)
QByteArray bytes;
bytes.append(((MsgItem->PIN)>>8) & 0xff);
bytes.append((MsgItem->PIN) & 0xff);
MsgFile->write(bytes);
MsgFile->flush();
Convert the enum value to a fixed-size integer (use
<cstdint>and e.g.uint16_t) and do whatever byte-ordering manipulations you need.