Linux Network Programming: in bytes (Large End, Small End, network, host), linux Large End

Source: Internet
Author: User

Linux Network Programming: in bytes (Large End, Small End, network, host), linux Large End

Byte order: the order in which data is stored in the memory. It can also be called the end mode.

Definition of big-end mode and small-end Mode

1)Little-EndianIt means that the low byte is discharged at the low address end of the memory, and the high byte is discharged at the high address end of the memory.

2)Big-EndianThat is, high bytes are discharged at the low address end of the memory, and low bytes are discharged at the high address end of the memory.

3)Network byte order: TCP/IP Protocols define byte orderBig-EndianTherefore, the byte order used in TCP/IP is usually called the network byte order.

What is a high byte or a low byte?

Generally, a 16-bit (dual-byte) data, such as FF1A (hexadecimal), is represented by a four-bit binary system in hexadecimal format)
So the high byte is FF, and the low byte is 1A.
For 32-bit data, such as 3f681_ B
The value of a high-level character (not a byte) is 3F68.
The low position is limit B.
Low on the right and high on the left

Include <stdio. h> # include <arpa/inet. h> int main (int argc, char * argv []) {unsigned int x = 0x12345678; unsigned char * p = (unsigned char *) & x; // small-end bytecode printf ("% p-> % 0x \ n % p-> % 0x \ n % p-> % 0x \ n % p-> % 0x \ n ", p + 0, * (p + 0), p + 1, * (p + 1), p + 2, * (p + 2), p + 3, * (p + 3); printf ("--------------------- \ n"); // h: Host byte order n: Network byte order l: long unsigned int y = htonl (x); p = (unsigned char *) & y; printf ("% p-> % 0x \ n % p-> % 0x \ n % p-> % 0x \ n % p-> % 0x \ n ", p + 0, * (p + 0), p + 1, * (p + 1), p + 2, * (p + 2), p + 3, * (p + 3); return 0 ;}

Output result:

0x7fff3bb55708-> 78
0x7fff3bb55709-> 56
0x7fff3bb5570a-> 34
0x7fff3bb5570b-> 12
---------------------
0x7fff3bb5570c-> 12
0x7fff3bb5570d-> 34
0x7fff3bb5570e-> 56
0x7fff3bb5570f-> 78

Result 1:

0x7fff3bb55708-> 78
0x7fff3bb55709-> 56
0x7fff3bb5570a-> 34
0x7fff3bb5570b-> 12

The address is increased from 08 ---> 0b. The low address of 08 is 78, and the number of 78 is low. In this case, the low address is put in the low byte, and the high address is put in the high byte, so it is called: Small-end byte order

Result 2:

0x7fff3bb5570c-> 12
0x7fff3bb5570d-> 34
0x7fff3bb5570e-> 56
0x7fff3bb5570f-> 78

Converts the host's byte order to the network's byte order. In this case, the low address places the high byte, and the high address places the low byte order.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------------

APIS related to byte sequence conversion:

Htonl: the host's byte order is converted to the network's byte order.

Inet_addr: function prototype: in_addr_t inet_addr (const char * cp)

Purpose: Convert the IP address format to network byte

unsigned int addr = inet_addr( "192.168.1.100" );printf( "%u\n", addr );printf( "0x%0x\n", addr );

Result:

1677830336
0x6401a8c0

The two numbers are the same.

Ntohl: Network byte order to host byte order

unsigned int haddr = ntohl( addr );printf( "%u\n", haddr );printf( "0x%0x\n", haddr );

Inet_ntoa: Network byte order, to IP address format

struct in_addr ipaddr;ipaddr.s_addr = addr;printf( "%s\n", inet_ntoa( ipaddr ) );

 

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.