What are the underlying transformations that are necessary to convert data in a little-endian system into network byte order? For 2 byte and 4 byte data there are well-known functions (such as htons, ntohl, etc.) to encapsulate the changes, what happens for strings of 1 byte data (if anything)?
Also, Wikipedia implies that little-endian is the mirror image of big-endian, but if that were true why would we need specific handling for 2 and 4 byte data?
The essay ‘On Holy Wars and a Plea for Peace’ seems to imply that there are many different flavors of little-endian — it’s an old essay — does that still apply? Are byte order markers like the ones found at the beginning of Java class files still necessary?
And finally, is 4-byte alignment necessary for network-byte order?
Let’s say you have the ASCII text ‘BigE’ in an array
bof bytes.This is network order for the string as well.
If it was treated as a 32 bit integer, it would be
on a little endian platform and
on a big endian platform.
If you convert each 16-bit work separately, you’d get neither of these
which is why
ntohlandntohsare both required.In other words,
ntohsswaps bytes within a 16-bit short, andntohlreverses the order of the four bytes of its 32-bit word.