bin2hex

(PHP 4, PHP 5, PHP 7)

bin2hex — 函数把包含数据的二进制字符串转换为十六进制值

说明

bin2hex ( string $str ) : string

把二进制的参数 str 转换为的十六进制的字符串。转换使用字节方式,高四位字节优先。

参数

str

二进制字符串。

返回值

返回指定字符串十六进制的表示。

参见

  • hex2bin() - 转换十六进制字符串为二进制字符串
  • pack() - 将数据打包成二进制字符串
    https://www.php.net/manual/zh/function.bin2hex.php

    hex2bin

    (PHP 5 >= 5.4.0, PHP 7)
    hex2bin — 转换十六进制字符串为二进制字符串

    说明

    hex2bin ( string $data ) : string
    转换十六进制字符串为二进制字符串。
    警告
    这个函数不是 转换十六进制数字为二进制数字。这种转换可以使用base_convert() 函数。

    参数

    data
    十六进制表示的数据

    返回值

    返回给定数据的二进制表示 或者在失败时返回 false

    错误/异常

    如果输入的十六进制字符串是奇数长数或者无效的十六进制字符串将会抛出 E_WARNING 级别的错误。

    更新日志

    | 版本 | 说明 | | :—- | :—- | | 5.5.1 | 如果输入的字符串是无效的十六进制字符串则抛出一个警告, | | 5.4.4 | 如果输入的字符串有多余将抛出异常。 PHP 5.4.0 起字符串将被静默地接受,但是最后的字节会被截断。 |

范例

示例 #1 hex2bin() 例子
<?php<br />$hex = hex2bin("6578616d706c65206865782064617461");<br />var_dump($hex);<br />?>
以上例程的输出类似于:
string(16) “example hex data”

pack

(PHP 4, PHP 5, PHP 7)
pack — 将数据打包成二进制字符串

说明

pack ( string $format [, mixed $... ] ) : string
将输入参数打包成 format 格式的二进制字符串。
这个函数的思想来自 Perl,所有格式化代码(format)的工作原理都与 Perl 相同。 但是,缺少了部分格式代码,比如 Perl 的 “u”。
注意,有符号值和无符号值之间的区别只影响函数 unpack(),在那些使用有符号和无符号格式代码的地方 pack() 函数产生相同的结果。

参数

format
format 字符串由格式代码组成,后面跟着一个可选的重复参数。重复参数可以是一个整数值或者 * 值来重复到输入数据的末尾。对于 a, A, h, H 格式化代码,其后的重复参数指定了给定数据将会被使用几个字符串。对于 @,其后的数字表示放置剩余数据的绝对定位(之前的数据将会被空字符串填充),对于其他所有内容,重复数量指定消耗多少个数据参数并将其打包到生成的二进制字符串中。
目前已实现的格式如下:
pack() 格式字符

代码 描述
a 以 NUL 字节填充字符串
A 以 SPACE(空格) 填充字符串
h 十六进制字符串,低位在前
H 十六进制字符串,高位在前
c 有符号字符
C 无符号字符
s 有符号短整型(16位,主机字节序)
S 无符号短整型(16位,主机字节序)
n 无符号短整型(16位,大端字节序)
v 无符号短整型(16位,小端字节序)
i 有符号整型(机器相关大小字节序)
I 无符号整型(机器相关大小字节序)
l 有符号长整型(32位,主机字节序)
L 无符号长整型(32位,主机字节序)
N 无符号长整型(32位,大端字节序)
V 无符号长整型(32位,小端字节序)
q 有符号长长整型(64位,主机字节序)
Q 无符号长长整型(64位,主机字节序)
J 无符号长长整型(64位,大端字节序)
P 无符号长长整型(64位,小端字节序)
f 单精度浮点型(机器相关大小)
g 单精度浮点型(机器相关大小,小端字节序)
G 单精度浮点型(机器相关大小,大端字节序)
d 双精度浮点型(机器相关大小)
e 双精度浮点型(机器相关大小,小端字节序)
E 双精度浮点型(机器相关大小,大端字节序)
x NUL 字节
X 回退已字节
Z 以 NUL 字节填充字符串空白(自 PHP 5.5 加入)
@ NUL 填充到绝对位置

...

返回值

返回包含数据的二进制字符串, 或者在失败时返回 false

更新日志

版本 说明
7.2.0 float 和 double 类型支持打断和小端。
7.0.15,7.1.1 添加了 “e”,“E”,“g” 和 “G” 代码以启用 float 和 double 的字节顺序支持。
5.6.3 添加了 “q”、“q”、“J” 和 “P” 代码以支持处理 64 位数字。
5.5.0 “Z” 代码添加了与 “a” 等效的功能,以实现 Perl 兼容性。

范例

示例 #1 pack() 范例
<?php<br />$binarydata = pack("nvc*", 0x1234, 0x5678, 65, 66);<br />?>
输出结果为长度为 6 字节的二进制字符串,包含以下序列 0x12, 0x34, 0x78, 0x56, 0x41, 0x42。

注释

警告
Note that PHP internally stores integer values as signed values of a machine-dependent size (C type long). Integer literals and operations that yield numbers outside the bounds of the integer type will be stored as float. When packing these floats as integers, they are first cast into the integer type. This may or may not result in the desired byte pattern.
The most relevant case is when packing unsigned numbers that would be representable with the integer type if it were unsigned. In systems where the integer type has a 32-bit size, the cast usually results in the same byte pattern as if the integer were unsigned (although this relies on implementation-defined unsigned to signed conversions, as per the C standard). In systems where the integer type has 64-bit size, the float most likely does not have a mantissa large enough to hold the value without loss of precision. If those systems also have a native 64-bit C int type (most UNIX-like systems don’t), the only way to use the I pack format in the upper range is to create integer negative values with the same byte representation as the desired unsigned value.

参见

  • unpack() - Unpack data from binary string

    unpack

    (PHP 4, PHP 5, PHP 7)
    unpack — Unpack data from binary string

    说明

    unpack ( string $format , string $data [, int $offset = 0 ] ) : array|false
    Unpacks from a binary string into an array according to the given format.
    The unpacked data is stored in an associative array. To accomplish this you have to name the different format codes and separate them by a slash /. If a repeater argument is present, then each of the array keys will have a sequence number behind the given name.

    参数

    format
    See pack() for an explanation of the format codes.
    data
    The packed data.
    offset
    The offset to begin unpacking from.

    返回值

    Returns an associative array containing unpacked elements of binary string, 或者在失败时返回 false.

    更新日志

    | 版本 | 说明 | | :—- | :—- | | 7.2.0 | float and double types supports both Big Endian and Little Endian. | | 7.1.0 | The optional offset has been added. | | 5.5.0 | Changes were made to bring this function into line with Perl:
    The “a” code now retains trailing NULL bytes.
    The “A” code now strips all trailing ASCII whitespace (spaces, tabs, newlines, carriage returns, and NULL bytes).
    The “Z” code was added for NULL-padded strings, and removes trailing NULL bytes. |

范例

示例 #1 unpack() example
<?php<br />$binarydata = "\x04\x00\xa0\x00";<br />$array = unpack("cchars/nint", $binarydata);<br />print_r($array);<br />?>
以上例程会输出:
Array
(
[chars] => 4
[int] => 160
)

示例 #2 unpack() example with a repeater
<?php<br />$binarydata = "\x04\x00\xa0\x00";<br />$array = unpack("c2chars/nint", $binarydata);<br />print_r($array);<br />?>
以上例程会输出:
Array
(
[chars1] => 4
[chars2] => 0
[int] => 40960
)

注释

警告
Note that PHP internally stores integral values as signed. If you unpack a large unsigned long and it is of the same size as PHP internally stored values the result will be a negative number even though unsigned unpacking was specified.
警告
If you do not name an element, numeric indices starting from 1 are used. Be aware that if you have more than one unnamed element, some data is overwritten because the numbering restarts from 1 for each element.
示例 #3 unpack() example with unnamed keys
<?php<br />$binarydata = "\x32\x42\x00\xa0";<br />$array = unpack("c2/n", $binarydata);<br />var_dump($array);<br />?>
以上例程会输出:
array(2) {
[1]=>
int(160)
[2]=>
int(66)
}

Note that the first value from the c specifier is overwritten by the first value from the n specifier.