大小端特点
小端序(Little-Endian)特点
特点:数据的最低有效字节(LSB)存储在最低内存地址,是现代主流架构。
大端序(Big-Endian)特点
特点:数据的最高有效字节(MSB)存储在最低内存地址,符合人类阅读习惯。
关键区别总结
|
对比项 |
小端序 |
大端序 |
|
存储顺序 |
低位字节在前( |
高位字节在前( |
|
优势 |
硬件运算高效,内存访问灵活 |
符合人类直觉,网络协议兼容 |
|
主流架构 |
x86、ARM、RISC-V |
PowerPC、SPARC、Motorola 68k |
代码检测方法
1. 强制类型转换法
#include <stdio.h>
void check_endian_cast() {
unsigned int num = 0x12345678;
unsigned char *ptr = (unsigned char *)#
printf("--- Method 1: Type Casting ---\n");
printf("Value: 0x%X\n", num);
printf("Bytes: [0x%X, 0x%X, 0x%X, 0x%X]\n", ptr[0], ptr[1], ptr[2], ptr[3]);
if (ptr[0] == 0x78) {
printf("Result: Little-Endian (LSB first)\n");
} else if (ptr[0] == 0x12) {
printf("Result: Big-Endian (MSB first)\n");
}
}
int main() {
check_endian_cast();
return 0;
}
输出示例(小端序):
--- Method 1: Type Casting ---
Value: 0x12345678
Bytes: [0x78, 0x56, 0x34, 0x12]
Result: Little-Endian (LSB first)
2. 数组遍历法
#include <stdio.h>
#include <string.h>
void check_endian_array() {
unsigned int num = 0x12345678;
unsigned char bytes[4];
memcpy(bytes, &num, 4); // 复制4字节到数组
printf("\n--- Method 2: Byte Array ---\n");
printf("Value: 0x%X\n", num);
printf("Bytes: [0x%X, 0x%X, 0x%X, 0x%X]\n", bytes[0], bytes[1], bytes[2], bytes[3]);
if (bytes[0] == 0x78) {
printf("Result: Little-Endian\n");
} else if (bytes[0] == 0x12) {
printf("Result: Big-Endian\n");
}
}
int main() {
check_endian_array();
return 0;
}
输出示例(小端序):
--- Method 2: Byte Array ---
Value: 0x12345678
Bytes: [0x78, 0x56, 0x34, 0x12]
Result: Little-Endian
3. 结构体法
#include<stdio.h>
typedef struct {
unsigned char b0, b1, b2, b3;
} IntBytes;
void check_endian_struct() {
unsigned int num = 0x12345678;
IntBytes *bytes = (IntBytes *)#
printf("\n--- Method 3: Struct ---\n");
printf("Value: 0x%X\n", num);
printf("Bytes: [0x%X, 0x%X, 0x%X, 0x%X]\n", bytes->b0, bytes->b1, bytes->b2, bytes->b3);
if (bytes->b0 == 0x78) {
printf("Result: Little-Endian\n");
} else if (bytes->b0 == 0x12) {
printf("Result: Big-Endian\n");
}
}
int main() {
check_endian_struct();
return 0;
}
输出示例(小端序):
--- Method 3: Struct ---
Value: 0x12345678
Bytes: [0x78, 0x56, 0x34, 0x12]
Result: Little-Endian
4. 联合体法
union EndianTest {
unsigned int num;
unsigned char bytes[4];
};
void check_endian_union() {
union EndianTest test;
test.num = 0x12345678;
printf("\n--- Method 4: Union ---\n");
printf("Value: 0x%X\n", test.num);
printf("Bytes: [0x%X, 0x%X, 0x%X, 0x%X]\n", test.bytes[0], test.bytes[1], test.bytes[2], test.bytes[3]);
if (test.bytes[0] == 0x78) {
printf("Result: Little-Endian\n");
} else if (test.bytes[0] == 0x12) {
printf("Result: Big-Endian\n");
}
}
int main() {
check_endian_union();
return 0;
}
输出示例(小端序):
--- Method 4: Union ---
Value: 0x12345678
Bytes: [0x78, 0x56, 0x34, 0x12]
Result: Little-Endian
5. 综合调用(完整程序)
#include <stdio.h>
#include <string.h>
// 方法1-4的函数定义(见上文)
int main() {
check_endian_cast(); // 强制类型转换
check_endian_array(); // 数组
check_endian_struct(); // 结构体
check_endian_union(); // 联合体
return 0;
}
--- Method 1: Type Casting ---
Value: 0x12345678
Bytes: [0x78, 0x56, 0x34, 0x12]
Result: Little-Endian (LSB first)
--- Method 2: Byte Array ---
Value: 0x12345678
Bytes: [0x78, 0x56, 0x34, 0x12]
Result: Little-Endian
--- Method 3: Struct ---
Value: 0x12345678
Bytes: [0x78, 0x56, 0x34, 0x12]
Result: Little-Endian
--- Method 4: Union ---
Value: 0x12345678
Bytes: [0x78, 0x56, 0x34, 0x12]
Result: Little-Endian
关键原理总结
|
方法 |
核心思想 |
优势 |
|
强制类型转换 |
通过指针直接访问内存字节 |
代码简洁,无需额外内存 |
|
数组 |
|
避免指针操作,更安全 |
|
结构体 |
利用结构体成员的内存对齐特性 |
直观反映字节顺序 |
|
联合体 |
共享内存空间,直接访问不同数据类型 |
天然适合检测字节序 |
注意事项:
- 所有方法均假设
int为 4 字节(32位系统)。 - 实际开发中优先使用联合体或强制类型转换(性能最佳)。
- 网络编程中需使用
htonl()/ntohl()等标准库函数处理字节序转换。
1099

被折叠的 条评论
为什么被折叠?



