当前位置:网站首页>主机字节序的判定
主机字节序的判定
2022-07-23 05:44:00 【lhb2998658795】
1.1字节序判定的基础知识以及判定
不同类型CPU的主机中,内存存储多字节整数序列有两种方法,称为主机字节序(HBO):
小端序(little-endian) - 的序字节存储在低地址
将低字节存储在起始地址,称为“Little-Endian”字节序,Intel、AMD等采用的是这种方式;
大端序(big-endian)- 高序字节存储在低地址
将高字节存储在起始地址,称为“Big-Endian”字节序,由ARM、Motorola等所采用
1.1.1字节序判定的原理图

1.1.2字节序判定的方法
#include <stdio.h>
int main(int argc, char const *argv[])
{
int num=0x12345678;
char *p=(char *)#//这个地方加强转防止爆警告
if(*p==0x12){
printf("这是大端字节序\n");
}else if(*p==0x78){
printf("这是小端字节序\n");
}
return 0;
}
union联合体是共用一块内存,所以可以使用这种方法测试
#include <stdio.h>
typedef union {
char a;
int b;
}msg_t;
int main(int argc, char const *argv[])
{
msg_t num;
num.b=0x12345678;
if(num.a==0x78){
printf("这是小端字节序\n");
}else if(num.a==0x12){
printf("这是大端字节序\n");
}
return 0;
}2.2什么情况下需要考虑字节序转换的问题
2.2.1判定情况
如果数据是1个字节的,无需考虑字节序的问题和如果明确知道通信的双方主机字节序一样,也可以不考虑,如果超过了1个字节的数据作为一个整体时,就需要考虑了。
2.2.2将小端序的无符号四字节整型转换成大端序,也就是转换原理
#include <stdio.h>
int main(int argc, char const *argv[])
{
unsigned int num=0x12345678;
char *p=(char *)#
char *q=p+3;
unsigned char temp;
temp=*p;
*p=*q;
*q=temp;
p++;
q--;
temp=*p;
*p=*q;
*q=temp;
printf("%#x\n",num);
return 0;
}2.3字节序转换函数
2.3.1函数
h host 主机 n network 网络 l 长 s 短
uint32_t htonl(uint32_t hostlong);//主机转网络 4字节
uint16_t htons(uint16_t hostshort);//主机转网络 2字节
uint32_t ntohl(uint32_t netlong);//网络转主机 4字节
uint16_t ntohs(uint16_t netshort);//网络转主机 2字节
2.3.2函数使用
#include <arpa/inet.h>
#include <stdio.h>
int main(int argc, char const *argv[])
{
unsigned int num=0x12345678;
unsigned int m=htonl(num);
printf("%#x\n",m);
return 0;
}
边栏推荐
- 高分子合成工艺学复习考题
- [AUTOSAR com 2. Advanced introduction to communication protocol stack]
- Blog building I: Framework selection
- Interpretation of the paper: "i4mc deep: intelligent prediction of N4 methylcytosine sites using deep learning methods with chemical properties"
- NLP natural language processing - Introduction to machine learning and natural language processing (2)
- Prometheus
- Deep learning neural network
- 求矩阵的鞍点及其对应的下标。
- Interpretation of the paper: "deep-4mcw2v: sequence based predictor for identifying N4 methylcytosine (4mc) sites in E. coli"
- [AUTOSAR DEM iv.event memory]
猜你喜欢

A comprehensive and detailed summary of the basic principles of steel structure

常见排序--归并排序(递归和非递归)+计数排序

钢结构基本原理题库

Blog building five: drawing bed selection

快速排序的按区间的三个版本及优化--友友们不一定了解

【Autosar DEM 四.Event Memory】

Blog Building II: next theme related settings beta
![[AUTOSAR com 1. introduction to communication protocol stack]](/img/ed/e61ca1e37817a81fc6eada54192876.png)
[AUTOSAR com 1. introduction to communication protocol stack]

高分子物理名词解释归纳
![[learning summary]](/img/f6/5f960052735a98057f66b7d186b53f.png)
[learning summary]
随机推荐
VS属性配置相关知识
博客搭建六:绑定自己域名的方法
常见的排序方法—选择排序
[AUTOSAR com 1. introduction to communication protocol stack]
【学习总结】
[AUTOSAR candrive 1. learn the function and structure of candrive]
表格个人简历
Review of basic principles of steel structure
switch实现表达式计算
解决谷歌chrome浏览器双击没反应,不能启动(亲测好用)
Blog building five: drawing bed selection
Anconda安装的pytorch依赖的cuda版本和系统cuda版本不一致问题
使用InfluxDB数据库的疑惑
Axure实现增删改查
用单向链表实现 队列
SCI审稿过程中的几种状态
配置历史版本Detectron遇到的问题
Interpretation of the paper: recognition of enhancer promoter interactions with neural networks based on pre trained DNA vectors and attention mechanisms
Questions and answers of basic principles of steel structure
Summary of video coding and decoding related data