24. Data Transmission

Back To All Submissions
Previous Submission
Next Submission

Code

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

void convert_to_big_endian(uint32_t value, uint8_t arr[4]) {
    // Your code here

    uint32_t mask = (~0U << 8);
    mask = ~mask;

    for ( int i = 0 ; i < 4 ; i++){
    arr[3-i] = (value & mask) >> (8*i); // we need >> (8*i) so that th eupper bits we are operaitng above come down to the first 8 since arr[i] is uint8_t //NB casting wont serve since the first 8 bits are still zeros and the worked on bits will just be ignored 
    mask = mask << 8;
    // printf("%u\n ", mask);
    }

}

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

 

 

 

Was this helpful?
Upvote
Downvote