54. Packet Layout Using Union with Struct

You’re implementing a communication packet for transmission. Each packet is 6 bytes structured as follows:

FieldSize (bytes)
Start Byte1
Command1
Data (2B)2
CRC1
End Byte1

 

Your task is to:

  • Define a union that overlays:
    • a struct view of these fields, and
    • a uint8_t[6] array view
  • Accept values for start, command, data (16-bit), CRC, and end
  • Fill the packet struct
  • Print the raw 6-byte array using the byte array view
     

Example-1

Input: start = 0xA5, cmd = 0x01, data = 0x1234, crc = 0x77, end = 0x5A
Output: 165 1 52 18 119 90

 

Example-2

Input: start = 0xAA, cmd = 0xFF, data = 0x00FF, crc = 0xFE, end = 0x55
Output: 170 255 255 0 254 85


 

Loading...

Input

165 1 4660 119 90

Expected Output

165 1 52 18 119 90