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

推荐订阅源

P
Privacy International News Feed
H
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Docker
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
月光博客
月光博客
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
C
Cisco Blogs
Microsoft Azure Blog
Microsoft Azure Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
LINUX DO - 热门话题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tor Project blog
M
MIT News - Artificial intelligence
L
LINUX DO - 最新话题
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
Security Archives - TechRepublic
Security Archives - TechRepublic
IT之家
IT之家
博客园_首页
F
Full Disclosure
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
阮一峰的网络日志
阮一峰的网络日志
Security Latest
Security Latest
S
Security @ Cisco Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AI
AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
A
Arctic Wolf
C
Check Point Blog
S
Securelist
A
About on SuperTechFans

博客园 - 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;
}