Overview:
- The function htons() converts a two-byte integer from host byte order to network byte order and returns the converted integer.
- The htonl() function provides the similar functionality and it works on a four-byte integer.
Example:
# Example Python program that converts a 16-bit intger print("Byte order of the host:%s"%sys.byteorder) # 16-bit integer to be converted # Convert the integer from host byte order # Print the 16-bit integer in host byte order # Print the bytes of the 16-bit integer whose byte order is changed to |
Output:
Byte order of the host:little 65534 65279 Given 16-bit integer in little endian format:b'\xfe\xff' Given 16-bit integer in big endian format:b'\xff\xfe' |