Little Endian to Big Endian

Code

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

uint32_t convert_endian(uint32_t value) {
    // Write logic to swap bytes
    uint32_t result = value;
    char* swap = (char*)&result;

    char temp;

    temp = swap[0];
    swap[0] = swap[3];
    swap[3] = temp;

    temp = swap[1];
    swap[1] = swap[2];
    swap[2] = temp;
    
    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