--- big-endian byte order is understood small end

Benpian will what, why, how three dimensions about endian

what

Endian, byte understood literally (Byte) sequence, computer science for a structure in a multi-byte (or variable files, etc.) convention , currently divided into big-endian and little-endian

In a  uint32_t . 4 byte type for example, 0x12345678 is stored in the variable type, variable from the point of view, is the high byte variable 0x12, 0x78 low byte variables.

When the variable  high  byte is stored in a memory of  the high  address, a variable  low  byte is stored in the memory  is low  when the address is little endian, as shown below:

 

When the variable  high  byte is stored in the memory  low  addresses, variable  low  byte is stored in a memory of  high  when the address as a big-endian, as shown below:

 

why

The definition simply agreed, of course, direct memory, but why should such a convention? Why divided into two byte sequence? what is the benefit?

Byte order for two kinds of two types of objects, big-endian  is intended for  humans , little-endian  is intended for  the computer .

Big endian human-readable diet, is defined in accordance with customary processing of reading a byte stream, so that the processing load can be reduced in humans, a network byte order is big endian use, when the network socket when transmitting a multi-bit number, in accordance with the reading habits, the number of received high byte, 8 thus directly adding the left.

Little-endian order processing conforming to the computer

        1. The computer reads data formats do not care, in accordance with the read only memory address order

        2. The computerized data processing from data low, this is the definition of computer design, so we need to lower variable data into the memory address of the active low address, so that the computer data processing is to take what we expect from the lower address Start. The accumulated operation time, bit addition, bit feed CF2, do ten added sequentially accumulating the final result.

In summary, the human-oriented big-endian order to simplify thinking, little endian oriented computer in order to speed up processing.

 

how

How to determine the computer is big-endian or little-endian?

You can simply make a judgment period of C language program

#include <cstdio>
#include <cstdint>

int main()
{
	uint32_t test_code = 0x12345678;
	uint8_t* first = (uint8_t*)&test_code;  //指针指向变量的内存首字节

	printf("first address value is %x,is %s\n", 
			*first, *first == 0x12 ? "Big Endian" : "Little Endian");
	return 0;
}

You can see output

first pointer to a first memory address of the variable test_code (lower memory addresses), the result was extracted 0x78 (variable low address), defined above as seen from the little-endian

 

In addition to directly view the output, we can see for yourself by looking at the way memory is stored, used here ollyDbg debug exe file above program debug mode output using ollyDbg open the exe file:

Right-search program in all the strings, string printf in here mainly to find, because it is not a simple encryption program, it is easy to find a place to call:

Directly view the address of a variable dump

And output program has been, with the low storage address 0x78, 0x12 with high storage address, therefore the little-endian.

 

In fact, the computer is not necessarily the little-endian, little-endian byte order computer just by default, if you need to change, you can set the corresponding control register settings, change the byte order to achieve the purpose, always remember just a byte order kinds of conventions.

 
Published 19 original articles · won praise 7 · views 6934

Guess you like

Origin blog.csdn.net/G_METHOD/article/details/104131628