C language big and small endian speedrun

1. Know the big and small ends

1. What is big and small endian?

Big endian is the storage method of machine memory , which is divided into big endian storage and small segment storage.

2. How is the specific big and small endian stored?

Big-endian: low-order addresses are stored in high-order memory
Small segments: low-order addresses are stored in low-order memory

Graphic:
insert image description here

3. Why do you need big and small endian?

Different computers store data in different ways. If two computers need to communicate, if the storage methods are different. One of the transmitted data is little-endian storage and the other is big-endian storage. The little endian sends the data "abcd" in the memory, and the big endian receives and puts the data in the memory as "dcba", so the data is messed up.

If the sender knows the size of the received data in advance, or agrees on the size of the end , then the data will not be wrong. For example: the original data in the little-endian memory is "abcd", but knowing that the receiving end is in the big-endian mode, the little-endian changes the sent data to "dcba", and the big-endian can accurately receive the original data "abcd" into the memory.

2. Use of big and small ends

1. Big and small endian judgment

The big and small endian judgment is based on the characteristics of the memory storage method . There are generally two judgment methods, one is the union method (need to know the storage characteristics of the union), and the other is the method of using pointers . Its core is to read the lowest/highest bit of data with more than two bits , and check whether the low-bit data is stored in the lowest/highest bit.

//判断大小端
//联合体法
union Dx
{
    
    
    int a;
    char c;
}un;
int judgeDx()
{
    
    
    un.a=1;
    if(un.c==1) return 0;//小端,内存低位存放低字节
    else return 1;//大端
}
//指针法
int judgePx()
{
    
    
    int a=1;
    char *c=(char *)&a;//获取数据低位
    if(*c==1) return 0;//小端,内存低位存放低字节
    else return 1;//大端
}

2. Big and small endian conversion

Big and small endian conversion is also performed using the storage characteristics of memory , and its core is to exchange positions . Put high-address memory into low-address memory, or put low-address memory into high-address memory.

Big and small endian conversions generally use shift operations , or other memory swap operations. Here I wrote a function that is not very efficient, but has good versatility for big and small endian conversion. The shift operation below can also improve learning.

/*移位法,效率相对高*/
#define SWP16(X)   (( ((uint16)(X) & 0xff00) >> 8)    | \  
(( (uint16)(X) & 0x00ff) << 8))  
 
#define SWP32(X)   ((( (uint32)(X) & 0xff000000) >> 24) | \  
(( (uint32)(X) & 0x00ff0000) >> 8)   | \  
(( (uint32)(X) & 0x0000ff00) << 8)   | \  
(( (uint32)(X) & 0x000000ff) << 24)) 

/*大小端转换,双指针法,通用性好,但需要注意内存越界*/
void LargeSmallConver(void *data,int size)
{
    
    
    unsigned char tmp;
    unsigned char *tdata=(unsigned char *)data;/*逐字节操作*/
    int hight=size-1,low=0;
    /*交换高低地址的数据*/
    while(low<hight)
    {
    
    
        tmp=tdata[low];
        tdata[low]=tdata[hight];
        tdata[hight]=tmp;
        low++;
        hight--;
    }
}

int main()
{
    
    
    int i;
    char test[4]={
    
    'a','b','c','d'};
    printf("before %s\r\n",test);//输出abcd
    LargeSmallConver(test,sizeof(test));
    printf("after %s\r\n",test);//dcba
    system("pause");
}

Diagram of double pointer method:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_46185705/article/details/126356318