当前位置:网站首页>Large end storage and small end storage

Large end storage and small end storage

2022-06-13 01:21:00 Zhao_| adult

1. What is big end and small end

  1. Little-Endian Namely Low bit byte Store in Low address side of memory , High byte Store in High address side of memory .
  2. Big-Endian Namely High byte Store in Low address side of memory , Low bit byte Store in High address side of memory .
    namely : Low low high is the small end , High low high is the big end ;

for example :0x12345678 The small end of is stored as :

Memory address The small end Big end
0x40000x780x12
0x40010x560x34
0x40020x340x56
0x40030x120x78

2. Why are there big and small end patterns ? Is the string divided into big and small ends ?

The unit of memory is byte , For characters ,char yes A byte , Not affected by host byte order and network byte order , There is only one unit in memory , There is no distinction between front and back . So for characters or strings , There is no difference between the big end and the small end ;
But when it's Combined memory space when , Because there are multiple memory units , There is Before and after , and The difference between the small end byte order and the large end byte order lies in how to combine the front and back memory units .

3. The size end of network communication

 Please add a picture description

The data operated during socket communication is stored at the big end , Include : receive / Data sent 、IP Address 、 port .
 Please add a picture description  Please add a picture description

 Please add a picture description

4. Size end judgment method :

#include <iostream>
#include <arpa/inet.h>
using namespace std;

// Determine whether the system is large-end storage or small-end storage : Can pass 'union  Consortium ' Judge ;
// Big end : The low byte is stored in the high address ;
// The small end : The low byte is stored in the low address ;
int Check_Big_End_Or_Small_End()
{
    
    union uni{
    
        uint16_t a;
        char c;
    };
    uni u;
    u.a=1;

    // because union It's a consortium , The memory size allocated by the compiler is the same as union The largest data types stored in are consistent ;
    // If it's big end , be t.c=0x00, be t.c!=1, return 0;
    // If it's small end , be t.c=0x00, be t.c==1, return 0;
    return (u.c==1);
}

// Convert the small end to the large end storage type ;
int transform()
{
    
    union uni{
    
        uint16_t a;
        char c;
    };
    uni u;
    u.a=1;

    // Small end storage is converted to large end storage ;

    u.a=htons(u.a);
    return (u.c==1);
}

int main()
{
    
    cout<<Check_Big_End_Or_Small_End()<<endl;//==> 1  The small end 
    cout<<transform()<<endl;//==> 0  Small end to large end 
    return 0;
}

5. Large and small end scenarios :

5.1 The Internet

For most programmers , The byte order used by the machine is completely invisible , We generally don't need to care about the order in which bytes are stored . however , Sometimes , Byte order can be a problem , First, in the Different types of Machine ( Most machines are small end storage ) Through between When the network transmits binary data , A common problem is when the data generated by the small end method machine is sent to the large end method machine , Or turn it upside down , When receiving a program , There will be chaos , To solve this problem , Web applications Your code must follow established rules about byte order , Ensure that the sender machine converts its internal representation into a network standard , The receiver machine transforms the network standard into its internal representation ;

author : Su Bingyu
link : https://subingwen.cn/linux/socket/#5-2-3-%E5%A5%97%E6%8E%A5%E5%AD%97%E5%87%BD%E6%95%B0
source : Da C who loves programming

原网站

版权声明
本文为[Zhao_| adult]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280553073332.html