Linux Network Programming 1 & mdash; Small-end mode and big-end mode, linux Network Programming big-end

Source: Internet
Author: User

Linux Network Programming 1-small-end mode and big-end mode, linux Network Programming big-end
Data storage priority Conversion

There are two types of computer data storage priorities: High-byte-first (big-end mode) and low-byte-first (small-end mode ). Low-address memory stores low bytes of data, and the high-byte mode of high-address storage data is called the small-end mode. The memory high address stores low data bytes. The low address stores high data bytes, which is called the big end mode.

Eg: For memory storage, 0x12345678 (note that for data, here 12 is a high byte, 78 is a low byte; for the address, the left side is a low address, high address on the right)

If it is stored in the big-end mode, the actual number is: 0x12345678.

If it is stored in the small-end mode, the actual number is: 0x78563412.

In summary, the small-end mode stores low bytes first, and the large-end mode stores high bytes first.

If a system adopts the host byte sequence, it may be in the small-end mode or large-end mode. Usually our computers store data in the small-end mode. Both the port number and IP address are stored in the network byte order, not the host byte order. The network byte order is in the big-end mode. To match the host's byte sequence with the network's byte sequence, the two bytes need to be converted to each other in priority.

Pra1 original question

Converts an IP address (in dotted decimal number) in string format to a 32-bit binary value in the host's byte order. The IP address is "180.97.33.107 ". The hexadecimal format is b4.61.21.6b.

Ideas

Generally, the host's byte order is the small-end byte order, that is, the low-byte storage data.

1. Extract each number in the string

2. Move left separately (note that 0 will be added when you move left)

180: 00 00 00 b4 shifts left 0 00 00 00 b4
97: 00 00 00 61 shift left 8 bits 00 00 61 00
33: 00 00 00 21 shifts left 16 bits 00 21 00
107: 00 00 6b shifts left 24 bits 6b 00 00 00

3. or operation

Code
/*************************************** * *********************************> File Name: my_atoh.c> Author: KrisChou> Mail: zhoujx0219@163.com> Created Time: wed 27 Aug 2014 08:52:38 pm cst ********************************* ***************************************/ # include <stdio. h> # include <stdlib. h> # include <string. h> # define IP "180.97.33.107"/* converts an ip address to a binary value in the host's byte sequence. The output value is in hexadecimal format */static int my_atoh (char * IP) {int arr [4];/* is used to store the four integers deducted from the IP address */int my_ip; sscanf (IP, "% d. % d. % d. % d ", arr, arr + 1, arr + 2, arr + 3); my_ip = (arr [3] <24) | (arr [2] <16) | (arr [1] <8) | arr [0]; return my_ip;} int main (int argc, char * argv []) {int my_host = my_atoh (IP); printf ("ip: % s \ n", IP); printf ("host: % x \ n", my_host ); return 0 ;}

The running result is as follows:

[purple@localhost 0827]$ gcc -o main my_atoh.c -Wall[purple@localhost 0827]$ ./mainip  : 180.97.33.107host: 6b2161b4[purple@localhost 0827]$
Note:

Note that when all scanf functions are formatted and read, % s is automatically added with '\ 0 '.

Pra2 original question

Converts the host's byte order (stored in the small-end mode) to the network's byte order (stored in the large-end mode ). That is, the host's byte order is 6b2161b4-> b461216b

Thought 1

Exchange 6b with b4, and 21 with 61.

Code
/*************************************** * *********************************> File Name: my_hton.c> Author: KrisChou> Mail: zhoujx0219@163.com> Created Time: wed 27 Aug 2014 09:26:13 pm cst ********************************* ***************************************/ # include <stdio. h> int my_hton (int ip) {/* & ip points to the integer ip address expressed in 32 bits, and converts the int * type pointer to the char * type pointer, * ptr points to the integer number (0-7 digits) expressed in 8 bits, and * ptr + 1 points to 8 bits (8th -15-bit) integer number. * ptr + 2 points to the integer number represented by 8 bits (16-23 digits, * ptr + 3 points to the integer number represented by 8 bits (24-31 bits. */
    char *ptr = (char*)&ip;        char tmp;    tmp = ptr[0];    ptr[0] = ptr[3];    ptr[3] = tmp;    tmp = ptr[1];    ptr[1] = ptr[2];    ptr[2] = tmp;    return ip;}int main(int argc, char *argv[]){    int my_host = 0x6b2161b4;    int my_net = my_hton(my_host);    printf("my_host: %x \n",my_host);    printf("my_net : %x \n",my_net);    return 0;}

The running result is as follows:

[purple@localhost 0827]$ gcc -o main my_hton.c -Wall[purple@localhost 0827]$ ./mainmy_host: 6b2161b4my_net : b461216b
Note:

In fact, the IP address is in dotted decimal format, with each byte representing a range of 0-255. Our char type usually has a signed number by default, that is, it indicates the range is-128-127. Is there an error in the yellow part of the code that I mark as the mark? In fact, there is nothing wrong, because here we only care about the storage of each bit of the ip address, and do not require the actual decimal value of each byte. When formatting the output with printf, the data is formatted in the binary form in the memory, while the hexadecimal form does not have a negative number.

Idea 2

1. For 0x6b2161b4, first shift right and perform operations with each byte.

For example, if you want to retrieve 6b, 0x6b2161b4 shifts to the right by 24 bits, and then performs operations with 0xff.

Note: do not perform operations first or right. For example, to retrieve 6b, perform and operate 0x6b2161b4 and 0xff000000, and then shift the value to the right by 24 digits. This is incorrect, because the right shift will fill in the symbol bit. (Tips: shifts left to 0, and removes the sign bit)

2. Move left to the appropriate position. (The following two steps are similar to Pra1)

3. or operation

Code
/*************************************************************************    > File Name: my_hton2.c    > Author: KrisChou    > Mail:zhoujx0219@163.com     > Created Time: Wed 27 Aug 2014 10:08:07 PM CST ************************************************************************/#include <stdio.h>int my_hton2(int ip){    int my_net;    my_net = ((ip&0xff)<<24) | (((ip>>8)&0xff) << 16) | (((ip>>16)&0xff) << 8) | ((ip>>24)&0xff);    return my_net;}int main(int argc, char *argv[]){    int my_host = 0x6b2161b4;    int my_net = my_hton2(my_host);    printf("my_host: %x \n", my_host);    printf("my_net : %x \n", my_net);    return 0;}
Pra3 original question

Converts the network byte order to dot decimal.

Code
/*************************************************************************    > File Name: my_ntoa.c    > Author: KrisChou    > Mail:zhoujx0219@163.com     > Created Time: Wed 27 Aug 2014 10:27:03 PM CST ************************************************************************/#include <stdio.h>static char* my_ntoa(int ip){    static char buf[1024] = "";    sprintf(buf,"%d.%d.%d.%d",(ip >> 24) & 0xff,(ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);    return buf;}int main(int argc,char *argv[]){    int my_net = 0xb461216b;    printf("my_net: %x \n", my_net);    printf("IP    : %s \n", my_ntoa(my_net));    return 0;}

Check the mode between small-end and large-end

Define a consortium.
Union {
Int;
Char B [4];
};
A. a = 1;
If (a. B [0] = 0)
{
// Large End
} Else {
// Small End
}

What is the big-end mode and small-end mode?

If a 32-bit integer 0x12345678 is stored in an integer variable (int), the storage of this integer variable in the memory is shown in the following table. For the sake of simplicity, this book uses OP0 to represent the highest Byte MSB (Most Significant Byte) of a 32-bit data, and OP3 to represent the lowest Byte LSB (Least Significant Byte ).

---------------------------
Address offset big-end mode small-end Mode
0x00 12 (OP0) 78 (OP3)
0x01 34 (OP1) 56 (OP2)
0x02 56 (OP2) 34 (OP1)
0x03 78 (OP3) 12 (OP0)
---------------------------

If you store a 16-bit integer 0x1234 in a short INTEGER (short. The small integer variables are stored in the memory in the size mode as shown in the following table.

---------------------------------
Address offset big-end mode small-end Mode
0x00 12 (OP0) 34 (OP1)
0x01 34 (OP1) 12 (OP0)
-------------------------------------
 

Related Article

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.