logoalt Hacker News

adrian_btoday at 6:20 PM0 repliesview on HN

Endianness problems should have been solved by compilers, not by programmers.

Most existing CPUs, have instructions to load and store memory data of various sizes into registers, while reversing the byte order.

So programs that work with big-endian data typically differ from those working with little-endian data just by replacing the load and store instructions.

Therefore you should have types like int16, int32, int64, int16_be, int32_be, int64_be, for little-endian integers and big-endian integers and the compiler should generate the appropriate code.

At least in the languages with user-defined data types and overloadable operators and functions, like C++, you can define these yourself, when the language does not provide them, instead of using ugly workarounds like htonl and the like, which can be very inefficient if the compiler is not clever enough to optimize them away.