In network programming, the network byte sequence is big-Endian, while most PC systems are x86 processor series, x86 uses little-Endian, so you need
Network data streams must be converted to local data streams in byte order.
The standard library provides two functions, hlton () and nthl (), to support conversion.
Hston (unsigned short) and hlton (unsigned long) convert local bytes to network bytes
Ntohl (unsigned long) and ntohs (unsigned short) convert the network byte order to the local byte order
However, for 64-bit integers, the standard library does not provide the corresponding conversion function. This article will provide an original 64-bit bytecode conversion function.
# Ifndef ulong64
# Define unsigned long ulong64
# Endif
// Host long 64 to Network
Ulong64 hl64ton (ulong64 host)
{
Ulong64 ret = 0;
Ulong high, low;
Low = Host & 0 xffffffff;
High = (host> 32) & 0 xffffffff;
Low = htonl (low );
High = htonl (high );
Ret = low;
RET <= 32;
RET | = high;
Return ret;
}
// Network to host long 64
Ulong64 ntohl64 (ulong64 host)
{
Ulong64 ret = 0;
Ulong high, low;
Low = Host & 0 xffffffff;
High = (host> 32) & 0 xffffffff;
Low = ntohl (low );
High = ntohl (high );
Ret = low;
RET <= 32;
RET | = high;
Return ret;
}
For more information about little endian and big endian, I will not go into detail in this article.