Packet Layout Using Union with Struct

Code

#include <stdio.h>
#include <stdint.h>


struct Packet
{
    uint8_t arr[6];
};


// Fill struct fields and print raw bytes
void build_and_print_packet(uint8_t s, uint8_t c, uint16_t d, uint8_t crc, uint8_t e) {
    // Your logic here
    struct Packet my_packet;
    my_packet.arr[0] = s;
    my_packet.arr[1] = c;
    my_packet.arr[2] = d & 0xFF;
    my_packet.arr[3] = (d >> 8) & 0xFF;;
    my_packet.arr[4] = crc;
    my_packet.arr[5] = e;

    for(int i=0; i<6; i++)
    {
        printf("%u ", my_packet.arr[i]);
    }
}

int main() {
    uint8_t s, c, crc, e;
    uint16_t d;
    scanf("%hhu %hhu %hu %hhu %hhu", &s, &c, &d, &crc, &e);
    build_and_print_packet(s, c, d, crc, e);
    return 0;
}//;

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

165 1 4660 119 90

Expected Output

165 1 52 18 119 90