I am not sure why htons
and ntohs
both exist in the standard library. They do exactly the same thing -- unless I'm confused somehow!
The same goes for htonl
and ntohl
.
I am not sure why htons
and ntohs
both exist in the standard library. They do exactly the same thing -- unless I'm confused somehow!
The same goes for htonl
and ntohl
.
They make for self-documenting code that tells the reader whether the data is in host- or network- order.
This is in case a machine has some sort of unusual endianness besides big-endian or little-endian that isn't one or more simple byte swaps.
For example, if the value 0x0A0B0C0D
was represented internally as 0B 0C 0D 0A
, then passing this representation to htonl
would return 0x0A0B0C0D
but ntohl
would return 0x0C0D0A0B
.
I'm not aware of any platforms that have such a representation, but the presence of separate functions for host-to-network and network-to-host allows for the possibility.
© 2022 - 2024 — McMap. All rights reserved.