📜  如何选择结构的字节序 cpp - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:45.490000             🧑  作者: Mango

代码示例1
#include 
#include 

//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);
}