45. Serialized Data Buffer

Back To All Submissions
Previous Submission
Next Submission

Code

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

void build_packet(uint8_t command, uint16_t value, uint8_t status, uint32_t checksum) {
    uint8_t buffer[10];
    for(int i=0; i<10; i++) {
        if(i==0) {
            buffer[i]=165;
        }
        else if(i==1) {
            buffer[i]=command;
        }
        else if(i==2) {
            buffer[i]=(value & 0xFF);
        }
        else if(i==3) {
            buffer[i]=(value >> 8) &0xFF;
        }
        else if(i==4) {
            buffer[i]=status;
        }
        else if(i==5) {
            buffer[i] = (checksum & 0xFF);
        }
        else if(i==6) {
            buffer[i] = (checksum >> 8) & 0xFF;
        }
        else if(i==7) {
            buffer[i] = (checksum >> 16) & 0xFF;
        }
        else if(i==8) {
            buffer[i] = (checksum >> 24) & 0xFF;
        }
        else {
            buffer[i] = 90;
        }
        printf("%d ", buffer[i]);
    }

    // Your logic to fill buffer
    // Then print buffer

}

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;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote