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

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

Sunshine

SpringBoot定时任务@Scheduled注解详解 | Sunshine 机器数、原码、反码、补码解析 | Sunshine Spring事件驱动模型实践 | Sunshine IDEA好用的几款插件 | Sunshine Linux使用yum安装MySQL详细步骤(CentOS7.3) | Sunshine Windows下MySQL解压版安装配置详细步骤 | Sunshine Java使用JDBC连接SQLServer数据库(二) | Sunshine Java使用JDBC连接SQLServer数据库(一) | Sunshine Git基本操作整理 | Sunshine Oracle序列创建和使用 | Sunshine Git批量清理本地分支 | Sunshine Statement.RETURN_GENERATED_KEYS获取自增id踩坑记录 | Sunshine GitPages+Hexo搭建个人博客 | Sunshine
大端模式、小端模式解析 | Sunshine
DongyangHu · 2021-02-25 · via Sunshine

大端模式、小端模式

计算机内存按字节编址,每个地址的存储单元可以存放8bit的数据。大小端面向多字节类型定义,比如2字节、4字节、8字节整型、长整型、浮点型等,单字节一般不用考虑。

查看当前机器大小端模式

各变成语言有不同的实现,Java可以通过如下代码获取当前机器的大小端模式

1
2

ByteOrder.nativeOrder();

大端模式

低地址存放高字节,高地址存放低字节。即将数据的低位放在高地址,高位放在低地址中

例:Java中一个int类型数据0x0000007f(十进制为127),4个字节,32位,7f为最低字节,其大端存储为

big-endian

小端模式

低地址存放低字节,高地址存放高字节。即将数据的低位放在低地址,高位放在高地址中

例:Java中一个int类型数据0x0000007f(十进制为127),4个字节,32位,7f为最低字节,其小端存储为

little-endian

代码示例

int类型数据转换为字节数组

程序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22



private static final int BASE_NUM = 0x0000007f;

public static void int2BytesTest() {
int2Bytes(BASE_NUM, ByteOrder.BIG_ENDIAN);
int2Bytes(BASE_NUM, ByteOrder.LITTLE_ENDIAN);
}







public static void int2Bytes(int num, ByteOrder byteOrder) {
final ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(byteOrder);
byte[] array = byteBuffer.putInt(num).array();
System.out.println("input:" + num + ", byteOrder:" + byteOrder + ", result:" + Arrays.toString(array));
}

输出结果

1
2
input:127, byteOrder:BIG_ENDIAN, result:[0, 0, 0, 127]
input:127, byteOrder:LITTLE_ENDIAN, result:[127, 0, 0, 0]

字节数组转int

还是以0x0000007f为例,程序代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28



private static final int BASE_NUM = 0x0000007f;

public static void bytes2IntTest() {

byte[] input1 = new byte[]{0, 0, 0, 127};
bytes2Int(input1, ByteOrder.BIG_ENDIAN);
bytes2Int(input1, ByteOrder.LITTLE_ENDIAN);

byte[] input2 = new byte[]{127, 0, 0, 0};
bytes2Int(input2, ByteOrder.BIG_ENDIAN);
bytes2Int(input2, ByteOrder.LITTLE_ENDIAN);
}







private static void bytes2Int(byte[] input, ByteOrder byteOrder) {
ByteBuffer byteBuffer = ByteBuffer.wrap(input, 0, 4);
byteBuffer.order(byteOrder);
int result = byteBuffer.getInt();
System.out.println("input:" + Arrays.toString(input) + ", byteOrder:" + byteOrder + ", result:" + result);
}

输出结果

1
2
3
4
input:[0, 0, 0, 127], byteOrder:BIG_ENDIAN, result:127
input:[0, 0, 0, 127], byteOrder:LITTLE_ENDIAN, result:2130706432
input:[127, 0, 0, 0], byteOrder:BIG_ENDIAN, result:2130706432
input:[127, 0, 0, 0], byteOrder:LITTLE_ENDIAN, result:127

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Sunshine