Htonl () function learning

Source: Internet
Author: User
Today, I saw an explanation of the htonl () function on the Internet, which makes sense. I will post it to you to learn it! Htonl is to convert the local bytes to the network byte order h --- host local host to N --- net network meaning L is unsigned long so-called network byte order (large tail order) it means that when a number is stored in the memory, "high to low, low to high" (that is, a high number of bytes is stored in a low address unit, low-level bytes are stored in High-address units ). However, the data stored in the computer's memory may be in the ascending or descending order. For example, int A = 0x403214; int B = htonl (a); I debugged this code in VC ++ 6.0 and found that the value of & A is: 0x0012ff44 the values of 0x0012ff44, 0x0012ff45, 0x0012ff46, and 0x0012ff47 are respectively 14, 32, 40, and 00, that is, the high part of 0x403214 is stored in the high address, the low part is stored in the low part address, that is, the Small Tail order. The value of & B is 0x0012ff40. The values of 0x0012ff40, 0x0012ff41, 0x0012ff42, and 0x0012ff43 are 00, 40, 32, and 14 in sequence, that is, the high part of the original number 0x403214 is stored in the low part, and the low part is stored in the high part. It can be seen that if a number is stored in ascending order, after the htonl function is called, the high-position bytes of this number will be completely reversed to a new number. This new number is actually stored in the machine in a small tail order, but it is equivalent to the original number, it is changed to a big tail order. The complete write of long 0x40 is 0x00 00 00 40, a total of four bytes. The order of the four bytes after calling htonl is 0x40 00 00. Similarly, after 0x40 00 00 calls htonl, it becomes 0x00 00 00 40, that is, 0x40

 

Htonl () function learning

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.