Answers for "how to choose struct's endianness cpp"

C++
0

how to choose struct's endianness cpp

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

//The line bellow does the trick:
//__attribute__((packed, scalar_storage_order("big-endian")))
struct __attribute__((packed, scalar_storage_order("big-endian"))) mystruct {
    uint16_t a;
    uint32_t b;
    uint64_t c;
};


int main(int argc, char** argv) {
    struct mystruct bar = {.a = 0xaabb, .b = 0xff0000aa, .c = 0xabcdefaabbccddee};

    FILE *f = fopen("out.bin", "wb");
    size_t written = fwrite(&bar, sizeof(struct mystruct), 1, f);
    fclose(f);
}
Posted by: Guest on April-30-2021

Browse Popular Code Answers by Language