byte order

SW           ARM       little-endion

BR100     mips                   big-engian

BR200     ppc            big-endian

 

大端(big endian):高位字节存储在低位地址中,从低地址看起,首先看到高位的字节,所以叫大端

 

小端(little endian):高位字节存储在高地址中,低位字节存储在低地址存储单元中,从低位看起,首先看到低位字节,所以叫小端

 

网络序是大端

主机序是小端

### Byte Order: Host and Network Byte Order In networking and programming, the term "byte order" refers to the sequence in which bytes are arranged within a multi-byte data type such as integers or floating-point numbers. The two primary byte orders are **big-endian** and **little-endian**, as described in the provided references[^1]. However, when discussing `byte order=host` and `byte order=0`, these terms typically relate to how systems handle byte ordering in network communication. #### Byte Order = Host The host byte order refers to the native byte order of the system on which the program is running. This could either be little-endian (e.g., Intel x86 architecture) or big-endian (e.g., SPARC, PowerPC). Programs that operate on a single machine often use the host's native byte order for efficiency because no conversion is required. However, this can lead to issues in networked environments where machines with different byte orders communicate with each other. For example: - On a little-endian machine, an integer value `0x12345678` would be stored in memory as `78 56 34 12`. - On a big-endian machine, the same integer would be stored as `12 34 56 78`. When working with host byte order, it is important to consider whether the data being processed will eventually need to be transmitted over a network, where a standardized byte order is typically used[^2]. #### Byte Order = 0 (Network Byte Order) Network byte order is a standard defined for data transmission across networks. It is always **big-endian**, meaning the most significant byte (MSB) comes first in the sequence. This ensures consistency across all devices communicating over the network, regardless of their native byte order. In many programming languages, especially those commonly used in networking (e.g., C/C++), functions exist to convert between host byte order and network byte order. For instance: - `htons()` (Host-to-Network Short): Converts a 16-bit integer from host byte order to network byte order. - `htonl()` (Host-to-Network Long): Converts a 32-bit integer from host byte order to network byte order. - `ntohs()` (Network-to-Host Short): Converts a 16-bit integer from network byte order to host byte order. - `ntohl()` (Network-to-Host Long): Converts a 32-bit integer from network byte order to host byte order. Here is an example of converting between host and network byte orders in C: ```c #include <stdio.h> #include <arpa/inet.h> int main() { uint16_t host_short = 0x1234; uint32_t host_long = 0x12345678; uint16_t net_short = htons(host_short); uint32_t net_long = htonl(host_long); printf("Host short: 0x%hx -> Network short: 0x%hx\n", host_short, net_short); printf("Host long: 0x%x -> Network long: 0x%x\n", host_long, net_long); return 0; } ``` This code demonstrates how values are converted between host and network byte orders using the provided functions. #### Key Differences 1. **Definition**: Host byte order refers to the native byte order of the system, while network byte order is always big-endian. 2. **Usage**: Host byte order is used for internal processing within a single machine, whereas network byte order is used for data transmission over networks to ensure compatibility. 3. **Conversion**: Functions like `htons()`, `htonl()`, `ntohs()`, and `ntohl()` are necessary when switching between host and network byte orders. --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值