Any way to read big endian data with little endian program?

前端 未结 8 1767
走了就别回头了
走了就别回头了 2021-01-13 13:50

An external group provides me with a file written on a Big Endian machine, and they also provide a C++ parser for the file format.

I only can run the parser on a lit

8条回答
  •  再見小時候
    2021-01-13 14:18

    Try persuading the parser team to include the following code:

    int getInt(char* bytes, int num)
    {
        int ret;
        assert(num == 4);
        ret = bytes[0] << 24;
        ret |= bytes[1] << 16;
        ret |= bytes[2] << 8;
        ret |= bytes[3];
        return ret;
    }
    

    it might be more time consuming than a general int i = *(reinterpret_cast<*int>(&myCharArray)); but will always get the endianness right on both big and small endian systems.

提交回复
热议问题