当前位置:网站首页>字节序 - 如何判断大端小端
字节序 - 如何判断大端小端
2022-06-12 11:23:00 【爱投资的偏执狂】
字节序 - 如何判断大端小端
大端模式:高的有效字节存储在低的存储器地址。网络字节序为大端;
小端模式:低的有效字节存储在低的存储器地址。
顺序自然即大端

判断原理:定义一个大字节的数据 x,判断存放 x 的首地址的数据如何。
强制类型转换,指针指向首地址
联合体中成员共用一段内存,并且都是从内存首地址开始存
/********************************************************* Copyright 2022 Shengkai Liu. All rights reserved. FileName: big_endian.c Author: Shengkai Liu Date: 2022-05-30 ***********************************************************/
#include <stdio.h>
// method one
void is_big_endian()
{
printf("方法一\n强制类型转换:\n");
int a = 0x11223344;
char b = *(char *)&a;
if (b == 0x11)
{
printf("大端模式\n");
}
else
{
printf("小端模式\n");
}
printf("\n");
}
// method two
void is_big_endian1()
{
printf("方法二\n联合体:\n");
union u
{
int a;
char b;
}u;
u.a = 0x11223344;
if (u.b == 0x11)
{
printf("大端模式\n");
}
else
{
printf("小端模式\n");
}
}
int main()
{
printf("size of int: %ld\n", sizeof(int));
printf("size of char: %ld\n\n", sizeof(char));
is_big_endian();
is_big_endian1();
return 0;
}
边栏推荐
猜你喜欢

套接字编程TCP篇

AcWing 131. The largest rectangle in the histogram (monotone stack classic application template)

Stream as a return value in WCF - who disposes of it- Stream as a return value in WCF - who disposes it?

信号继电器RXSF1-RK271018DC110V

你需要社交媒体二维码的21个理由

Windows10安装mysql-8.0.28-winx64

深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)

当自己有台服务器之后

Clickhouse column basic data type description

C# 35. 选择默认网卡
随机推荐
AcWing 41. 包含min函数的栈(单调栈)
The most detailed explanation of the top ten levels of sqli labs platform
Handwritten common interview questions
35. 搜索插入位置
Golang基础(6)
redis 總結
^34作用域面试题
MySQL performance test (slow query log)
AcWing 1986. 镜子(模拟,环图)
Differences among various cross compiling tools of arm
Pytorch笔记
Drqueueonrails integrated LDAP authentication
DS18B20数字温度计 (一) 电气特性, 寄生供电模式和远距离接线
人类想要拥有金钱、权力、美丽、永生、幸福……但海龟只想做一只海龟
AcWing 131. 直方图中最大的矩形(单调栈经典运用 模板)
FormatConversionTool.exe
K58. Chapter 1 installing kubernetes V1.23 based on kubeadm -- cluster deployment
Distributed storage exploration
AcWing 1986. Mirror (simulation, ring diagram)
套接字编程TCP篇