Home  >  Article  >  Backend Development  >  PHP byte order conversion method

PHP byte order conversion method

藏色散人
藏色散人Original
2020-08-20 09:58:523605browse

php byte order conversion method: 1. Use the "htons" method to convert the unsigned short type from host order to network order; 2. Use "htonl" to convert the "unsigned long" type from host order to network order. Network sequence, etc.

PHP byte order conversion method

Recommended: "PHP Video Tutorial"

php host byte order and network byte order

When using PHP to write a socket program, you also need to pay attention to the conversion between host byte order and network byte order.

Host byte order is what we usually call big endian and little endian. Mode: Different CPUs have different endianness types. These endiannesses refer to the order in which integers are stored in memory. This is called host order. Big-Endian and Little-Endian. The standard definitions of Big-Endian and Little-Endian are quoted as follows:

a) Little-Endian means that the low-order bytes are arranged at the low address end of the memory, and the high-order bytes are arranged at the high address end of the memory.

b) Big-Endian means that the high-order bytes are arranged at the low address end of the memory, and the low-order bytes are arranged at the high address end of the memory.

Example: The storage method of double word 0x01020304 (DWORD) in memory

内存地址
4000 4001 4002 4003
LE 04 03 02 01
BE 01 02 03 04

Network byte order refers to a data representation format specified in TCP/IP, which is related to the specific It has nothing to do with CPU type, operating system, etc., thus ensuring that data can be interpreted correctly when transmitted between different hosts. Network byte order uses big endian sorting. Byte order, as the name implies, is the order in which data larger than one byte is stored in memory. There is no order issue with data of one byte.

In order to convert bsd socket provides the following four conversion functions

htons converts the unsigned short type from host sequence to network sequence

htonl converts the unsigned long type from Convert host order to network order

ntohs Convert unsigned short type from network order to host order

ntohl Convert unsigned long type from network order to host order

When using In little endian systems, these functions will convert the byte order

In systems using big endian types, these functions will be defined as empty macros

Note:

 1 , Network and host byte conversion function: htons ntohs htonl ntohl (s is short l is long h is host n is network)

 2. Different operating systems run on different CPUs, and the byte order is also different.

When developing network programs or cross-platform development, you should also pay attention to ensure that only one byte order is used. Otherwise, different interpretations by the two parties will cause bugs.

PHP merge Functions that do not provide htons and htonl functions can be encoded and decoded through

string pack ( string $format [, mixed $args [, mixed $... ]] )
array unpack ( string $format , string $data )

during network communication.

Among them, format can take the following values:

Code     Description
a     NUL-padded string
A     SPACE-padded string
h     Hex string, low nibble first
H     Hex string, high nibble first
c     signed char
C     unsigned char
s     signed short (always 16 bit, machine byte order)
S     unsigned short (always 16 bit, machine byte order)
n     unsigned short (always 16 bit, big endian byte order)
v     unsigned short (always 16 bit, little endian byte order)
i     signed integer (machine dependent size and byte order)
I     unsigned integer (machine dependent size and byte order)
l     signed long (always 32 bit, machine byte order)
L     unsigned long (always 32 bit, machine byte order)
N     unsigned long (always 32 bit, big endian byte order)
V     unsigned long (always 32 bit, little endian byte order)
f     float (machine dependent size and representation)
d     double (machine dependent size and representation)
x     NUL byte
X     Back up one byte
Z     NUL-padded string (new in PHP 5.5)
@     NUL-fill to absolute position

Network Communication

For example, now we need to send data packets to the server through PHP to log in. When you only need to provide the username (up to 30 bytes) and password (fixed to 32 bytes after md5), you can construct the following data packet (of course, this requires negotiating the specifications of the data packet with the server in advance. In this example Network byte order communication):

Packet structure:

字段  字节数 说明
包头  定长         每一个通信消息必须包含的内容
包体  不定长 根据每个通信消息的不同产生变化

The details of the header are as follows:

字段               字节数 类型       说明
pkg_len         2     ushort     整个包的长度,不超过4K
version         1     uchar     通讯协议版本号
command_id 2     ushort     消息命令ID
result         2     short     请求时不起作用;请求返回时使用

Of course, various verifications may be involved in practice. For the sake of simplicity, this article only lists the common workflow and processing methods.

Login (execution command 1001)

字段       字节数 类型         说明
用户名 30     uchar[30] 登录用户名
密码         32     uchar[32] 登录密码

The header is of fixed length. Through calculation, it can be seen that the header occupies 7 bytes, and the header is before the package body. For example, user Chen Yihui needs to log in and the password is 123456, the code is as follows:

<?php
$version    = 1;
$result     = 0;
$command_id = 1001;
$username   = "陈一回";
$password   = md5("123456");
// 构造包体
$bin_body   = pack("a30a32", $username, $password);
// 包体长度
$body_len   = strlen($bin_body);
$bin_head   = pack("nCns", $body_len, $version, $command_id, $result); //result为什么使用了主机字节序
$bin_data   = $bin_head . $bin_body;
// 发送数据
// socket_write($socket, $bin_data, strlen($bin_data));
// socket_close($socket);

The above is the detailed content of PHP byte order conversion method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn