1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
#include <cstdint>
#include <cstring>
using namespace std;
#define DEFAULT_SCHEDULE {0x44, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74, 0x20, 0x53, 0x63,\
0x68, 0x65, 0x64, 0x75, 0x6C, 0x65, 0x00, 0x00, 0x00, 0x00,\
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x10, 0x00, 0xFE, 0x02,\
0xA4, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
0x00, 0x00, 0x00, 0x00}
//Note: Bytes were added to make length of data a multiple of sizeof(struct)
typedef struct
{
char label[34];
uint8_t start_hour;
uint8_t start_minute;
uint8_t stop_hour;
uint8_t stop_minute;
uint8_t dayofweek; // bit 1 = sunday.... bit 7 = saturday
uint8_t active; // 0 = uninitialized, 1 = enabled, 2 = disabled
int16_t rpm;
} schedule_span_t;
int main()
{
unsigned char data[] = DEFAULT_SCHEDULE;
std::cout << "sizeof(data): " << sizeof(data) << '\n';
std::cout << "sizeof(schedule_span_t): " << sizeof(schedule_span_t) << '\n';
schedule_span_t arr[2];
std::cout << "sizeof(schedule_span_t arr[2]): " << sizeof(arr) << '\n';
memcpy(arr, data, sizeof(arr));
for (int i = 0; i < 2; i++)
{
bool null_terminated = false;
for (int j = 0; j < 34; j++)
{
if (arr[i].label[j] == '\0')
{
null_terminated = true;
break;
}
}
if (null_terminated)
{
std::cout << "Label " << i << ": \"" << arr[i].label << "\"\n";
}
else
{
std::cout << "Warning: Label " << i << " is not properly null-terminated\n";
}
}
}
|