我需要将整数转换为8位,但pack只能转换为16位二进制数据。我在谷歌上搜索,却找不到蚂蚁。有人能帮忙吗?
echo $test = pack('C', 1);
根据格式将给定的参数打包为二进制字符串。
这个函数的想法取自Perl,所有格式化代码的工作方式与Perl相同。然而,也有一些格式代码缺失,例如Perl的"u"格式代码。
注意,有符号值和无符号值之间的区别只影响函数unpack(),其中函数pack(
代码为
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
@ NUL-fill to absolute position
您可以使用c或c。类似:
pack('C', 234);
c是有符号的,c是无符号的。
要获得正确的格式并添加像这样的前导zéros 00000001使用这个:
<?php
// Add leading zeros
$bin = sprintf( "%08d", decbin( 26 )); // "00011010"
?>
中的答案http://php.net/manual/en/function.decbin.php