Little Endian to Big Endian

Code

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

    // char *lower = (char*)&value;
    // char *higher = (char*)&value;
    // higher += 3;
    // while(lower < higher){
    //     char temp = *lower;
    //     *lower = *higher;
    //     *higher = temp;

    //     lower++;
    //     higher--;
    // }
uint32_t convert_endian(uint32_t value) {
    uint32_t res = 0;
    res |= (value & 0xFF);
    res = res << 8;
    res |= (value >> 8) & 0xFF;
    res <<= 8;
    res |= (value >> 16) & 0xFF;
    res <<= 8;
    res |= (value >> 24) & 0xFF;

    return res;
}

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