You can throw a bunch of hton* (Host TO Network) and ntoh* (Network TO Host) calls in there between all of your different uses. Network endian is Big Endian.
16 bit:
htons = Host TO Network Short
ntohs = Network TO Host Short
32 bit:
htonl = Host TO Network Long
ntohl = Network to Host Long
Really both host to network and network to host are the same because the same swapping happens either way.
They are generally implemented as macros and will be no-ops on platforms that use big endian.
They live in:
#include
Which is generally available.
You could also make use of
if your libraries have it. With gcc this requires -D_BSD_SOURCE
On Unix, BSD, and Linux try:
man htons
man endian
man byteorder