How to write endian agnostic C/C++ code?

前端 未结 7 816
我在风中等你
我在风中等你 2020-12-08 00:40

I did some googling and couldn\'t find any good article on this question. What should I watch out for when implementing an app that I want to be endian-agnostic?

7条回答
  •  無奈伤痛
    2020-12-08 01:12

    This might be a good article for you to read: The byte order fallacy

    The byte order of the computer doesn't matter much at all except to compiler writers and the like, who fuss over allocation of bytes of memory mapped to register pieces. Chances are you're not a compiler writer, so the computer's byte order shouldn't matter to you one bit.

    Notice the phrase "computer's byte order". What does matter is the byte order of a peripheral or encoded data stream, but--and this is the key point--the byte order of the computer doing the processing is irrelevant to the processing of the data itself. If the data stream encodes values with byte order B, then the algorithm to decode the value on computer with byte order C should be about B, not about the relationship between B and C.

提交回复
热议问题