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
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.
- The input types are the same as the output types, instead of referring to a byte stream.
- This requires the user to perform a pointer typecast which is possibly unsafe in alignment.
- Having performed that typecast, the user is likely to attempt to convert and output a structure in its native memory layout, a poor practice which results in unexpected errors.
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?
hton?
/ntoh?
and size in name, the size actually is in the name. The last letter is the size,s
forshort
andl
forlong
. – Servialong
is 64 bits on many systems, and a system with non-8-bit bytes is unlikely to match exactly 16-bitshort
or 32-bitlong
. The C standard is purposely ambiguous about it. – Animation