I'm using Java sockets to exchange data with a server written in C. How can I convert my integer values to Network Byte Order so the C server can read them properly?

Tim Rohaly

Because different operating systems have different integer representations, when you send integers over a socket from a C or C++ program it is important to use the functions htons(), htonl(), ntohs(), and ntohl() to convert to and from Network Byte Order. This machine-independent standard network representation ensures that different operating systems can exchange data.

I/O in Java always uses Network Byte Order, so there is no need for conversion, and no equivalent of the above functions.