79. Extract Packet Fields from Byte Stream

Back To All Submissions
Previous Submission
Next Submission

Code

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

typedef struct __attribute__((packed)){
    uint8_t start;
    uint8_t command;
    uint16_t data;
    uint8_t crc;
    uint8_t end;
} Packet;
Packet p;

//165 1 52 18 119 90
void print_packet_fields(uint8_t *buffer) {
    // Overlay struct and print values
    Packet *p_ptr = (Packet*)buffer;
    printf("Start: %d\n", p_ptr->start );
    printf("Command: %d\n", p_ptr->command );
    printf("Data: %d\n", p_ptr->data );
    printf("CRC: %d\n", p_ptr->crc );
    printf("End: %d\n", p_ptr->end );
}

int main() {
    uint8_t buffer[6];
    for (int i = 0; i < 6; i++) {
        scanf("%hhu", &buffer[i]);
    }
    print_packet_fields(buffer);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote