Converting Endianess on a bit field structure

前端 未结 8 699
心在旅途
心在旅途 2021-02-02 03:48

I need to convert a bit-field structure from little-endian to big-endia architecture. What is the best way to do that, as there will be issues in byte boundaries, if I simply sw

8条回答
  •  无人共我
    2021-02-02 04:21

    You could use a 32 bit integer, and extract information out of it using and- and bitshift operators. With that in place, you could simply use htonl (host-to-network, long). Network byte order is big endian.

    This won't be as elegant as a bit-field, but at least you'll know what you have and won't have to worry about the compiler padding your structures.

提交回复
热议问题