All submissions

Little Endian to Big Endian

Code

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

static void swap(uint8_t* b1, uint8_t* b2)
{
    uint8_t temp;
    temp = *b1;
    *b1 = *b2;
    *b2 = temp;
}

uint32_t convert_endian(uint32_t value) {
    // Write logic to swap bytes
    unsigned char* start = (unsigned char*)&value;
    unsigned char* end = (unsigned char*)((unsigned char*)&value + sizeof(value) - 1);
    swap(start, end);
    ++start;
    --end;
    swap(start, end);
    return value;
}

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

Solving Approach


 

Loading...

Input

305419896

Expected Output

2018915346