Endian - Big Endian and Little Endian

A byte order

Endian order refers to a multi-byte data stored in the memory

 

Memory address high end and the low end address. Wherein the low end address may be stored upper byte, the lower byte can be stored.

  • Big Endian refers to the low address end of the high byte storage.
  • Little Endian refers to the low address end of the low byte storage.

 

Second, why should note endian

If you write a program to run only in stand-alone environment below, and others do not, and procedures to deal with, then you can ignore the existence of byte order.

However, if your program not want anybody else to produce interactive programs do? For example, when a C / C ++ program to interact with a Java program:

  • Program data stored in the order written C / C ++ language is compiled with CPU platform where relevant, and now more common x86 processor is Little Endian

  • Programs written in JAVA using Big Endian is the only way to store data

Just think, if your C / C ++ program variables  a = 0x12345678 passed to the first address of the Java program, Java due to take Big Endian way to store data, it is natural that it will be translated into your data  0x78563412. Obviously, the question arises! ! !

Further, network transmission generally use Big Endian, also known as network byte order , or the network order . When two hosts communicate using a different byte order, before transmitting the converted data byte sequence must be transmitted before the network byte order.

 

Third, the network host Sequence Sequence

Network byte order : TCP / IP protocol layers is defined as a sequence of bytes Big Endian, so endian TCP / IP protocol used is big endian.

Host byte order : integer in the sequence stored in memory, is now more common Little Endian. (Different CPU has a different byte order)

Typically you need to call the appropriate function to convert the host and network order sequence during network communications. Berkeley socket API defines a set of conversion functions for 32bit integer transform 16 and between the network and the sequence native byte order. htonl, htons sequence for native sequence converted to the network; ntohl, ntohs order for the network to convert the native sequence.

 

233


https://songlee24.github.io/2015/05/02/endianess/

Guess you like

Origin www.cnblogs.com/lemos/p/12004074.html