Code

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

void convert_to_big_endian(uint32_t value, uint8_t arr[4]) {
    // Shift right to bring the desired byte to the LSB position, 
    // then cast/mask to 8 bits.
    arr[0] = (uint8_t)((value >> 24) & 0xFF);
    arr[1] = (uint8_t)((value >> 16) & 0xFF);
    arr[2] = (uint8_t)((value >> 8) & 0xFF);
    arr[3] = (uint8_t)(value & 0xFF);
}

int main() {
    uint32_t value;
    uint8_t arr[4];
    
    // Using %u for unsigned int; for hex input, you'd use %x
    if (scanf("%u", &value) != 1) return 1;

    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

 

 

 

Upvote
Downvote
Loading...

Input

305419896

Expected Output

18 52 86 120