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

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
Google DeepMind News
Google DeepMind News
腾讯CDC
博客园 - 司徒正美
Cisco Talos Blog
Cisco Talos Blog
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
博客园 - 聂微东
博客园 - 【当耐特】
Project Zero
Project Zero
有赞技术团队
有赞技术团队
量子位
P
Privacy International News Feed
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
J
Java Code Geeks
IT之家
IT之家
SecWiki News
SecWiki News
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
L
Lohrmann on Cybersecurity
宝玉的分享
宝玉的分享
Cloudbric
Cloudbric
雷峰网
雷峰网
月光博客
月光博客
Cyberwarzone
Cyberwarzone
S
Securelist
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - Franky
T
Threat Research - Cisco Blogs
罗磊的独立博客
Forbes - Security
Forbes - Security
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Troy Hunt's Blog
Jina AI
Jina AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cyber Attacks, Cyber Crime and Cyber Security
The Last Watchdog
The Last Watchdog
V2EX - 技术
V2EX - 技术

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