当前位置:网站首页>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-Endian
Namely Low bit byte Store in Low address side of memory , High byte Store in High address side of memory .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 |
---|---|---|
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
边栏推荐
- ES6 deconstruction assignment
- FSOs forest simulation optimization model learning notes
- np.concatenate中axis的理解
- Get preview of precast body
- Leetcode-14- longest common prefix (simple)
- Tangent and tangent plane
- Liu Hui and introduction to nine chapter arithmetic and island arithmetic
- HashSet underlying source code
- Wikipedia API User Guide
- Set and array conversion, list, array
猜你喜欢
使用Pygame创建一个简单游戏界面
论文笔记:STMARL: A Spatio-Temporal Multi-AgentReinforcement Learning Approach for Cooperative Traffic
[projet cs144 de Stanford Computing Network] lab1: Stream reassembler
Ecological convergence NFT attacks, metaverse ape leads the new paradigm revolution of Web 3.0 meta universe
Plusieurs catégories de tests logiciels sont claires à première vue
【斯坦福计网CS144项目】Lab1: StreamReassembler
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income
Addition and modification of JPA
Jenkins持续集成操作
C language implementation of the classic eight queens problem
随机推荐
How to print infinite symbol in WPS
spiral matrix visit Search a 2D Matrix
leetcode. 541. reverse string II
Unity extension
使用Pygame创建一个简单游戏界面
Traditional machine learning classification model predicts the rise and fall of stock prices under more than 40 indicators
Happy string
Application advantages of 5g industrial gateway in coal industry
5G工业网关在煤矿行业的应用优势
Leetcode-14- longest common prefix (simple)
Wangdao machine test - Chapter 6 - maximum common divisor GCD and minimum common multiple LCM
Minimum spanning tree problem
My crawler learning notes
Leetcode-19- delete the penultimate node of the linked list (medium)
五篇经典好文,值得一看
MySQL transaction
Database query user mailbox
Key point detection data preparation and model design based on u-net Network -- detection model of four key points of industrial components
MySQL异常:com.mysql.jdbc.PacketTooBigException: Packet for query is too large(4223215 > 4194304)
Method of cleaning C disk