Code

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

/*
    arr[0] = Most significan Byte (Leftmost Byte)
    .
    .
    .
    arr[3] = Least Significant Byte (Rightmost Byte)

    To ahive this we need to shift right the vlaue by Byte offset as given bellow

    Byte        Offset
     1            24
     2            16
     3            8
     4            0

    we have used for loop ittrating from 0-3 to get the Byte offset value for each iteration, we need
    to do the following math,

    (3-iterration no)*8
*/

void convert_to_big_endian(uint32_t value, uint8_t arr[4]) {
    // Your code here
    uint32_t mask = 0xFF,i=0;
    for(i=0;i<4;i++)
    {
        arr[i] = (mask & (value >> ((3-i)*8)));
    }
    
}

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

 

 

 

Upvote
Downvote
Loading...

Input

305419896

Expected Output

18 52 86 120