Conversion of host byte order to network byte order: Ntohl () and htonl ()

Source: Internet
Author: User

#!/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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.