当前位置:网站首页>Determination of host byte order
Determination of host byte order
2022-07-24 01:00:00 【lhb2998658795】
1.1 Basic knowledge of byte order determination and determination
Different types of CPU In the mainframe of , There are two ways to store multi byte integer sequences in memory , It is called host byte order (HBO):
Small end of the sequence (little-endian) - The order bytes of are stored in the low address
Store the low byte at the starting address , be called “Little-Endian” Byte order ,Intel、AMD And so on in this way ;
big-endian (big-endian)- High order bytes are stored in low address
Store the high byte at the starting address , be called “Big-Endian” Byte order , from ARM、Motorola Etc
1.1.1 Schematic diagram of byte order determination

1.1.2 Method of byte order determination
#include <stdio.h>
int main(int argc, char const *argv[])
{
int num=0x12345678;
char *p=(char *)#// This place strengthens the explosion prevention warning
if(*p==0x12){
printf(" This is big endian \n");
}else if(*p==0x78){
printf(" This is the small endian byte order \n");
}
return 0;
}
union The consortium shares a piece of memory , So you can use this method to test
#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(" This is the small endian byte order \n");
}else if(num.a==0x12){
printf(" This is big endian \n");
}
return 0;
}2.2 Under what circumstances do we need to consider the problem of byte order conversion
2.2.1 Judge the situation
If the data is 1 Bytes of , There is no need to consider the problem of byte order, which is the same as if the byte order of the hosts on both sides of the communication is clearly known , You can also ignore , If you exceed 1 Bytes of data as a whole , You need to think about it .
2.2.2 Convert the unsigned four byte integer of small endian to large endian , That is, the principle of transformation
#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 Byte order conversion function
2.3.1 function
h host host n network The Internet l Long s short
uint32_t htonl(uint32_t hostlong);// Host to network 4 byte
uint16_t htons(uint16_t hostshort);// Host to network 2 byte
uint32_t ntohl(uint32_t netlong);// Network to host 4 byte
uint16_t ntohs(uint16_t netshort);// Network to host 2 byte
2.3.2 Function USES
#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;
}
边栏推荐
- [STM32] basic knowledge of serial communication
- Centernet target detection model and centerfusion fusion target detection model
- C language: deep analysis of const keyword
- scroll-view实现下拉刷新(避免onload进入页面初始refresher-triggered为true触发下拉问题)
- 暑假第四周总结
- Bean Validation自定义容器验证篇----06
- ACL——net
- [reply] about the fact that I chose the wrong technology at the wrong time
- 测试小码农也有大目标,最新BAT大厂面试题大总结(持续更新中...)
- 入职3个月的测试员面临转正,领导:1年工作经验包装成5年,试用期淘汰
猜你喜欢

Seektiger's okaleido has a big move. Will the STI of ecological pass break out?

Hypothesis test of Pearson correlation coefficient

Design details related to sap e-commerce cloud Spartacus UI store

Solve the problem that MySQL inserts Chinese garbled code into the table

GLIB-CRITICAL g_ file_ test:assertion ‘filename != null‘ failed

Introduction to QT (2.1 the first procedure for the beginning of QT)

C language: deep analysis of const keyword

Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes

網絡系統實驗:ping不通的問題解决

This is a big problem
随机推荐
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
Jianzhi offer 05 two stacks to realize the queue
freemarker
Sword *offer04 rebuild binary tree
Chapter 1 water test --*offer
Image processing: Generation 3 × Window of 3
Starfish OS: create a new paradigm of the meta universe with reality as the link
JS determines whether the element scrolls to the top
PostgreSQL snapshot optimization globalvis new system analysis (greatly enhanced performance)
Establishment of static route
MySQL's heart index
Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“
[the 83rd fortnight of leetcode]
NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘
VLAN division, automatic allocation of IP to all hosts through DHCP, and communication accessible throughout the network
Modify node temporarily_ Modules package source code and compile reference to modify dependent packages
J'ai choisi la mauvaise technologie au mauvais moment.
Bean Validation使用篇----05
The high-quality digital collection of guochuang's "children's song line" is on sale, and you are invited to create a young martial arts Jianghu dream
Expérience du système réseau: résoudre les problèmes de ping