惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
罗磊的独立博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
The Hacker News
The Hacker News
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
F
Full Disclosure
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
H
Hacker News: Front Page
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
B
Blog RSS Feed
H
Heimdal Security Blog
Google Online Security Blog
Google Online Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
V2EX - 技术
V2EX - 技术
V
Vulnerabilities – Threatpost
Help Net Security
Help Net Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
W
WeLiveSecurity
T
Tenable Blog
D
DataBreaches.Net
Martin Fowler
Martin Fowler
Cyberwarzone
Cyberwarzone
Cisco Talos Blog
Cisco Talos Blog
S
Secure Thoughts
O
OpenAI News
L
LINUX DO - 热门话题
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
J
Java Code Geeks
Know Your Adversary
Know Your Adversary
IT之家
IT之家
Latest news
Latest news
Cloudbric
Cloudbric

博客园 - peter he

LPCTSTR c++ 资源文件操作 - peter he Unicode-enabling Microsoft C/C++ Source Code HeapAlloc和GlobalAlloc以及VirtualAlloc三者之间的关系 电脑运行中死机解决方案<经典> The difference between Delegate and Composite The most effiective core chinese journals Some ideas 电池缩水 操作系统缩水 显卡缩水(时钟频率、显存大小、显卡接口、显存位宽) 非Firefox浏览器检测组件 个人习惯培养计划 房价和肉价反应的真实社会 C++ Review Series 答辩开始 洗心革面, 重新开始 从“老公”的称呼来历,看男人地位的变迁 嵌入式系统 Boot Loader 技术内幕
big-endian和little-endian
peter he · 2007-10-29 · via 博客园 - peter he

所谓的big-endian(大尾)和little-endian(小尾)是针对数据在内存中存储的字节序而言的. 我举个例子:
int a = 1;
a这个数本身的16进制表示是0x00 00 00 01
在内存中怎么存储呢?
如果你的CPU是intel x86架构的(基本上就是通常我们说的奔腾cpu),那么就是0x01 0x00 0x00 0x00 , 这也就是所谓的little-endian, 低字节存放在内存的低位.
如果你的CPU是老式AMD系列的(很老很老的那种,因为最新的AMD系列已经是x86架构了), 它的字节序就是big-endian, 其内存存储就是
0x00 0x00 0x00 0x01在内存中从高字节开始存放。
现在世界上绝大多数的CPU都是little-endian。

了解big-endian和little-endian有什么作用?一个重要的作用就是了解在网络上不同的机器间的数据如何传输。
假设我们在网络上有两台机器A和B, 其中A为little-endian,B为big-endian
机器A要传输上面的整数a给机器B,如何传输呢?
过程是这样的:
机器A先把a在内存中的四个字节

0x 01 0x00 0x00 0x00

转化为网络字节序

0x00 0x00 0x00 0x01

,然后一个字节一个字节(从0x00到0x01)喂到网络上去
然后机器B从网络上一个字节一个字节地取出四个字节

0x00 0x00 0x00 0x01

后又会转化为本地字节序

0x00 0x00 0x00 0x01

后放入内存。因而B正确地得到了来自A的数据a

如果数据缺少在网络上的字节序转换的话,情况会怎样呢?
机器A先把a由在内存的四个字节

0x 01 0x00 0x00 0x00

 一个字节一个字节地喂到网络上,然后机器B从网络上一个字节一个字节地收到

0x 01 0x00 0x00 0x00

并放入到内存中, B认为他收到了0x01000000, 也就是十进制数1677216,这显然是错误的.


如何判断你的机器是什么字节序呢?
下面给出一个简单的函数

inline bool IsPlatformLittleEndian()
{
   
const int n = 1;
   
return *((char*)&n) ? true : false;
}

 如何在big-endian和little-endian之间转换呢?
参考下面的函数

template <typename T>
void ReverseBytes(T& n)
{
    unsigned 
char* p = (unsigned char*)&n;
    std::reverse(p, p 
+ sizeof(T));    
}

下面给出一个测试的例子 

#include <iostream>
#include 
<algorithm>
using namespace std;

inline 
bool IsPlatformLittleEndian()
{
    
const int n = 1;
    
return *((char*)&n) ? true : false;
}


template 
<typename T>
void ReverseBytes(T& n)
{
    unsigned 
char* p = (unsigned char*)&n;
    std::reverse(p, p 
+ sizeof(T));    
}


void main()
{
    cout 
<< "Your CPU is " << (IsPlatformLittleEndian() ? "Little" : "Big"<< "-Endian" << endl;
    
int n = 1;
    ReverseBytes(n);
    cout 
<< n << endl;
}