#include <stdio.h>
#include <stdint.h>
enum {
PKT_START_IDX = 0,
PKT_COMMAND_IDX = 1,
PKT_VALUE_IDX = 2, // bytes 2..3
PKT_STATUS_IDX = 4,
PKT_CHECKSUM_IDX = 5, // bytes 5..8
PKT_END_IDX = 9,
};
#define PKT_START_BYTE ((uint8_t)0xA5)
#define PKT_END_BYTE ((uint8_t)0x5A)
static void write_u16_le(uint8_t *dst, uint16_t value)
{
for (int i = 0; i < 2u; i++)
{
dst[i] = (uint8_t)((value >> (i * 8)) & 0xFFu);
}
}
static void write_u32_le(uint8_t *dst, uint32_t value)
{
for (int i = 0; i < 4u; i++)
{
dst[i] = (uint8_t)((value >> (i * 8)) & 0xFFu);
}
}
void build_packet(uint8_t command, uint16_t value, uint8_t status, uint32_t checksum) {
uint8_t buffer[10];
// Your logic to fill buffer
buffer[PKT_START_IDX] = PKT_START_BYTE;
buffer[PKT_COMMAND_IDX] = command;
write_u16_le(&buffer[PKT_VALUE_IDX], value);
buffer[PKT_STATUS_IDX] = status;
write_u32_le(&buffer[PKT_CHECKSUM_IDX], checksum);
buffer[PKT_END_IDX] = PKT_END_BYTE;
// Then print buffer
for(int i = 0; i < 10; i++)
{
printf("%d ", buffer[i]);
}
}
int main() {
uint8_t cmd, status;
uint16_t val;
uint32_t crc;
scanf("%hhu %hu %hhu %u", &cmd, &val, &status, &crc);
build_packet(cmd, val, status, crc);
return 0;
}
Input
1 4660 1 2864434397
Expected Output
165 1 52 18 1 221 204 187 170 90