#!/usr/bin/env python#coding=utf-8import socketdef Convert_integer ():d ata=1234#32-bitprint "Original:%s = Long Host byte order:%s, Network byte order:%s "% (Data,socket.ntohl (data), SOCKET.HTONL (data)) #16-bitprint" Original:%s = > Short host byte order:%s, Network byte order:%s "% (Data,socket.ntohs (data), socket.htons (data)) if __name__== ' __main __ ': Convert_integer ()
Operation Result:
32-bit:
1234d=0xd2040000
Windows (Small-end method: Low-bit byte at low address):
Addr addr+1 addr+2 addr+3
0x D2 04 00 00
Ntoh (): assumes that the given data is a network byte order (big-endian: low byte at high address):
Inside Windows is stored in the form:
Addr addr+1 addr+2 addr+3
0x D2 04 00 00 (as the big-endian method)
Converted: xx D2
Read it under Windows (small End method):
0xd2040000=3523477504
Hton (): assumes that the given data is the host byte order (small end method under Windows):
Inside Windows is stored in the form:
Addr addr+1 addr+2 addr+3
0x D2 04 00 00 (as a small-end method)
Converted: xx D2
Read it under Windows (small End method):
0xd2040000=3523477504
The same can be analyzed under 16-bit conditions.
Conversion of host byte order to network byte order: Ntohl () and htonl ()