David Brown said:
One feature I would love to see in the standards - which would require
more work from the compiler and not just a typedef - is to have defined
integer types that specify explicitly big-endian or little-endian
layout. Non-two's-complement systems are rare enough to be relegated to
history, but there are lots of big-endian and little-endian systems, and
lots of data formats with each type of layout. I have used a compiler
with this as an extension feature, and it was very useful.
[...]
Implementing arithmetic on foreign-endian integers strikes me as
a waste of time. If I need to read and write big-endian integers
on a little-endian machine (a common requirement, since network
protocols typically often big-endian), I can just convert big-
to little-endian on input and little- to big-endian on output.
And POSIX provides htonl, htons, ntohl, and ntohs for exactly that
purpose ("h" for host, "n" for network).
There are no such functions that will convert between big-endian
and little-endian on a big-endian system, but such conversions are
rarely needed (and it's easy enough to roll your own if necessary).
https://en.wikipedia.org/wiki/Endianness
It might be a waste of time for /your/ sort of programming, but it is
not a waste of time in embedded systems. I don't often need to convert
endianness, but it certainly happens on occasion - and compilers that
have extensions allowing data to be declared in a particular format make
it easier to write clearer code in such cases. It means the format
choice is made at the declaration of the data (or data types), avoiding
extra function call syntax when the data is used, and avoiding the
possibility that the function call syntax is forgotten. It also means
that you write more portable code, because it is easy and cheap to
declare the endianness of transferred data even if you know it is the
same as your current target.
Posix functions don't help here - we are a /long/ way from posix on such
systems. And the necessity of specifying the sizes in the posix
functions is an extra layer of complication on embedded systems, where
such transferred data often has varying data sizes. And of course,
there are plenty of times when you might want to access little-endian on
big-endian architectures, for which Posix does not help.
Different types of programming needs (or wants) different features - a
standards defined way of declaring the endianness of data and types
would be a useful feature for many people, and save a lot of
roll-your-own solutions.