当前位置:网站首页>Large end storage and small end storage
Large end storage and small end storage
2022-06-13 01:21:00 【Zhao_| adult】
List of articles
1. What is big end and small end
Little-EndianNamely Low bit byte Store in Low address side of memory , High byte Store in High address side of memory .Big-EndianNamely 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 |
|---|---|---|
| 0x4000 | 0x78 | 0x12 |
| 0x4001 | 0x56 | 0x34 |
| 0x4002 | 0x34 | 0x56 |
| 0x4003 | 0x12 | 0x78 |
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

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


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
边栏推荐
- Minimum spanning tree problem
- Method of cleaning C disk
- Leetcode-78- subset (medium)
- What kind of experience is it to be a software test engineer in a state-owned enterprise: every day is like a war
- Leetcode 01 array
- Jenkins持续集成操作
- [Latex] 插入圖片
- Et5.0 simply transform referencecollectorieditor
- 软件测试的几种分类,一看就明了
- Rasa dialogue robot helpdesk (III)
猜你喜欢

切线与切平面

redis

Sliding window summary of TCP connections

My crawler learning notes

What kind of experience is it to be a software test engineer in a state-owned enterprise: every day is like a war

Application advantages of 5g industrial gateway in coal industry
![[Stanford Jiwang cs144 project] lab1: streamreassembler](/img/7b/fad18b68a6ee30d1dec4dad6273b98.png)
[Stanford Jiwang cs144 project] lab1: streamreassembler

项目实训(十七)---个人工作总结

DFS and BFS notes (II): depth first search (implemented in C language)

Summary of various installation methods of Lab View
随机推荐
Happy string
Wikipedia API User Guide
About_ int128
Deadlock problem summary
np.concatenate中axis的理解
[projet cs144 de Stanford Computing Network] lab1: Stream reassembler
五篇经典好文,值得一看
Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
Lessons learned from the NLP part of the course "Baidu architects hands-on deep learning"
C language implementation of the classic eight queens problem
304. Merge two ordered arrays
DFS and BFS notes (II): depth first search (implemented in C language)
Leetcode 05 tree
Work and life
Most elements leetcode
Jenkins持续集成操作
Introduction to convolutional neural network
Alexnet实现Caltech101数据集图像分类(pytorch实现)
How does Apple add QQ email?
Status of the thread