Is there a way to enforce specific endianness for a C or C++ struct?

前端 未结 11 1191
南方客
南方客 2020-12-13 09:24

I\'ve seen a few questions and answers regarding to the endianness of structs, but they were about detecting the endianness of a system, or converting data between the two d

11条回答
  •  囚心锁ツ
    2020-12-13 09:54

    Boost provides endian buffers for this.

    For example:

    #include 
    #include 
    
    using namespace boost::endian;
    
    struct header {
        big_int32_buf_t     file_code;
        big_int32_buf_t     file_length;
        little_int32_buf_t  version;
        little_int32_buf_t  shape_type;
    };
    BOOST_STATIC_ASSERT(sizeof(h) == 16U);
    

提交回复
热议问题