Little Endian to Big Endian

Code

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

uint32_t convert_endian(uint32_t value) {
    // Write logic to swap bytes
    uint8_t bytes[4];

    // Extract each byte and store it in reverse order
    bytes[3] = (uint8_t)  (value & 0x000000FF);
    bytes[2] = (uint8_t) ((value & 0x0000FF00) >> 8);
    bytes[1] = (uint8_t) ((value & 0x00FF0000) >> 16);
    bytes[0] = (uint8_t) ((value & 0xFF000000) >> 24);

    // Extract the combined 32-bit unsigned value
    uint32_t * result = (uint32_t *) bytes;
    
    return (*result);
}

int main() {
    uint32_t val;
    scanf("%u", &val);
    printf("%u", convert_endian(val));
    return 0;
}

Solving Approach
 

Upvote
Downvote
Loading...

Input

305419896

Expected Output

2018915346