Portable C binary serialization primitives
===

As far as I know, the C library provides no help in serializing numeric values into a non-text byte stream. Correct me if I'm wrong.

The most standard tool in use is [`htonl`][1] et al from POSIX. These functions have shortcomings:

- There is no 64-bit support.
- There is no floating-point support.
- There are no versions for signed types. When deserializing, the unsigned-to-signed conversion relies on signed integral overflow which is UB.
- Their names do not state the size of the datatype.
- They depend on 8-bit bytes and the presence of exact-size uint_*N*_t.
- They operate in place, which encourages the poor practice of using them within a `struct`.
 - Padding in such a `struct` interferes with serialization.
 - The `struct` is left invalid after in-place conversion.

An interface for serializing arbitrary-size `char` to 8-bit standard bytes would fall in between the C standard, which doesn't really acknowledge 8-bit bytes, and whatever standards (ITU?) set the octet as the fundamental unit of transmission. But the older standards aren't getting revised.

Now that C11 has many optional components, a binary serialization extension could be added alongside things like threads without placing demands on existing implementations.

Would such an extension be useful, or is worrying about non-two's-complement machines just that pointless?

  [1]: http://pubs.opengroup.org/onlinepubs/9699919799/functions/ntohl.html