how to choose struct 27s endianness cpp

Solutions on MaxInterview for how to choose struct 27s endianness cpp by the best coders in the world

showing results for - "how to choose struct 27s endianness cpp"
Mila
10 Mar 2020
1#include <stdio.h>
2#include <stdint.h>
3
4//The line bellow does the trick:
5//__attribute__((packed, scalar_storage_order("big-endian")))
6struct __attribute__((packed, scalar_storage_order("big-endian"))) mystruct {
7    uint16_t a;
8    uint32_t b;
9    uint64_t c;
10};
11
12
13int main(int argc, char** argv) {
14    struct mystruct bar = {.a = 0xaabb, .b = 0xff0000aa, .c = 0xabcdefaabbccddee};
15
16    FILE *f = fopen("out.bin", "wb");
17    size_t written = fwrite(&bar, sizeof(struct mystruct), 1, f);
18    fclose(f);
19}
20
similar questions
queries leading to this page
how to choose struct 27s endianness cpp