/*
* 文件名: Ending.c
* 文件描述: 检测当前平台是大端存储还是小端存储, 同时把大小端数据逆置
* 大端存储:高数据为在低地址 小端存储:低数据位在低地址
* 编写人: 王廷云
* 编写日期: 2017-10-17
*/
#include <stdio.h>
/* 使用联合来证明平台大小端 */
union store_u {
int value;
char ch[4];
};
int main(void)
{
union store_u test;
test.value = 0x44332211; // 只有大于一个字节的数据才有大小端的区别
printf("整型值为: 0x%x\n", test.value);
printf("整型的第一个字节值为: 0x%x\n", test.ch[0]);
/* 判断大小端 */
if (test.ch[0] == 0x11)
{
printf("所以当前平台为: 小端存储\n");
}
else if (test.ch[0] == 0x44)
{
printf("所以当前平台为: 大端存储\n");
}
/* 逆置数据 */
int i;
char temp;
char *ptr = (char *)&(test.value);
for (i = 0; i < sizeof(test.value)/2; i++)
{
temp = ptr[i];
ptr[i] = ptr[sizeof(test.value)-1-i];
ptr[sizeof(test.value)-1-i] = temp;
}
printf("逆置大小端数据后: 0x%x\n",test.value);
return 0;
}
【C语言】之实现检测平台大小端
最新推荐文章于 2024-05-07 11:10:16 发布