All submissions

Data Transmission

Code

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

bool is_big_endian_system() {
        int i = 0x01;
        char *c = (char *)(&i);

        // Big endian - MSB at lowest address
        //c = 0x00 00 00 01

        if (*c == 0) {
                // big endian
                return true;
        }
        return false;
}

void convert_to_big_endian(uint32_t value, uint8_t arr[4]) {
        // Your code here
        char *c = (char *) (&value);
        if (is_big_endian_system()) {
                arr[0] = *c;
                arr[1] = *(c+1);
                arr[2] = *(c+2);
                arr[3] = *(c+3);
        } else {
                arr[3] = *c;
                arr[2] = *(c+1);
                arr[1] = *(c+2);
                arr[0] = *(c+3);
        }
}

int main() {
        uint32_t value;
        uint8_t arr[4];
        scanf("%u", &value);
        convert_to_big_endian(value, arr);
        for (int i = 0; i < 4; i++) {
                printf("%u", arr[i]);
                if(i<3){
                        printf(" ");
                }
        }
        return 0;
}

Solving Approach

 

 

 

Loading...

Input

305419896

Expected Output

18 52 86 120