Converting Endianess on a bit field structure

前端 未结 8 673
心在旅途
心在旅途 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条回答
  •  梦毁少年i
    2021-02-02 04:10

    It should be enough to swap the bytes. Bit position within a byte is the same in big and little endian.
    e.g. :

    char* dest = (char*)&yourstruct;
    unsigned int orig = yourstruct;
    char* origbytes = (char*)&orig;
    dest[0] = origbytes[3];
    dest[1] = origbytes[2];
    dest[2] = origbytes[1];
    dest[3] = origbytes[0];
    

提交回复
热议问题