Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Long integer 4 bytes to decimal.
Long integer 4 bytes to decimal.
Anyone who has done socket knows the network byte conversion. The data transmitted in the network is a pure byte stream, without type information, and is transmitted from a low address. The network byte order is usually the big end, that is, the high byte is transmitted first, so the storage order is the same as the local byte at the big end, but the opposite is true at the small end. For the consistency of data, it is necessary to convert local data into the format used on the network, and then send it out, and the same is true when receiving it. After conversion, these data can be used. Basic library functions all provide such functions that can convert bytes, such as and htons() htonl() ntohs() ntohl (), where n stands for network, h stands for host, htons() htonl () is used to convert local bytes to network bytes, s stands for short, that is, 2-byte operation, and l stands for long, that is, 4-byte operation. Similarly, ntohs( )ntohl () is used when network bytes are converted to local format. With the implementation of the c99 standard, new types of long long int and unsigned long long int have been added to our big C, both of which are 64-bit. What do we do? If we don't turn, we have to find a way to turn ourselves. Of course, there are many ways. Here I want to use an analogy solution to see how to draw inferences.

I. Definition of byte order

Byte order, as the name implies, is the order of bytes, and a few more words are the storage order of data larger than one byte type in memory. In fact, most people rarely deal with byte order directly in actual development. Only in cross-platform and network programs should byte order be considered. The platform migration from Sun SPARC to Intel X86 caused our program to encounter "byte order problem".

In all articles introducing byte order, it will be mentioned that byte order is divided into two categories: big end and small end. The big end and small end of the reference standard are defined as follows:

A) Little-Endian means that the low byte is released at the low address end of the memory, and the high byte is released at the high address end of the memory.

B) Big-Endian means that the high byte is discharged at the low address end of the memory, and the low byte is discharged at the high address end of the memory.

C) network byte order: TCP/IP protocol defines byte order as Big-Endian, so the byte order used in TCP/IP protocol is usually called network byte order.