Code

// You are preparing a 32-bit value to send over a communication bus.
// To ensure compatibility across platforms, 
// you must convert the value into 4 bytes (big-endian order) and store them into a byte array

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

void convert_uint32_reg(uint32_t reg, uint8_t arr[4]){
    arr[0] = (uint8_t)((reg >> 24) & 0x00FF); 
    arr[1] = (uint8_t)((reg >> 16) & 0x00FF); 
    arr[2] = (uint8_t)((reg >> 8) & 0x00FF); 
    arr[3] = (uint8_t)((reg >> 0) & 0x00FF); 
}

uint8_t arr[4]; 

int main(){
    uint32_t reg; 
    scanf("%u",&reg); 
    convert_uint32_reg(reg,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