#include <stdio.h> #include <stdint.h> #include <string.h> void convert_to_big_endian(uint32_t value, uint8_t arr[4]) { uint32_t forth_byte_mask = 0x000000ff; uint32_t third_byte_mask = 0x0000ff00; uint32_t second_byte_mask = 0x00ff0000; uint32_t first_byte_mask = 0xff000000; arr[0] = (value & first_byte_mask)>>(32-8); arr[1] = (value & second_byte_mask)>>(32-16); arr[2] = (value & third_byte_mask) >>(32-24); arr[3] = (value & forth_byte_mask) >>(32-32); } 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; }
Mask the value with a preset mask values then store it in buffer
Test Cases
Test Results
Input
305419896
Expected Output
18 52 86 120