114. Little Endian to Big Endian

Back To All Submissions
Previous Submission
Next Submission

Code

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

uint32_t convert_endian(uint32_t value) {
    // Write logic to swap bytes
    uint8_t byte0 = (value >> 0)&0xFF;
    uint8_t byte1 = (value >> 8)&0xFF;
    uint8_t byte2 = (value >> 16)&0xFF;
    uint8_t byte3 = (value >> 24)&0xFF;
    uint32_t convert = (byte0 << 24)| (byte1 << 16) | (byte2 << 8) | byte3;

    return convert;
}

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

Solving Approach


 

Was this helpful?
Upvote
Downvote