Overview:
- The function ntohl() converts a 32-bit integer from network byter order to host byte order and returns the converted integer.
- The network byter order is big endian. Chips from Intel, AMD and Apple M1, M2 and M3 chips use little endian architecture. The IBM z/Architecture uses big endian architecture.
- The ntohl() does the inverse of what htonl() function does. When the host and the network byte order are the same, the ntohl() function returns the given parameter without any conversion.
Example:
# Example Python program that converts a 32 bit integer import socket # Number used for host versus network order conversion # Print the byte order of the host system numBytes = num.to_bytes(4, byteorder = 'little') # Get the network byte order # Get the host byte order print("Output of ntohl - host byte order:%s"%htOrder) |
Output:
Byte order of the host system:little Number in host byte order - before any conversion:b'\xfc\xff\x0f\x00' Output of htonl - network byte order:b'\x00\x0f\xff\xfc' Output of ntohl - host byte order:b'\xfc\xff\x0f\x00' |