I would like to know more about the pack()
function in PHP: https://secure.php.net/manual/en/function.pack.php
我想更多地了解PHP中的pack()函数:https://secure.php.net/manual/en/function.pack.php
I know it packs data into binary, but I'm not sure what all those v V n N c C
mean and I was wondering if someone could be so kind as to give me a practical demonstration when to use which formats?
我知道它将数据打包成二进制文件,但我不确定所有那些v V n N c C是什么意思,我想知道是否有人可以如此善良地给我一个实际演示何时使用哪种格式?
The online documentation, for a change, lacks of information, in my opinion.
在我看来,在线文档的变更缺乏信息。
2 个解决方案
#1
21
Those represent how you want the data you are packing to be represented in binary format:
这些表示您希望包装的数据以二进制格式表示的方式:
so
所以
$bin = pack("v", 1); => 0000000000000001
(16bit)
$ bin = pack(“v”,1); => 0000000000000001(16位)
where
哪里
$bin = pack("V", 1) => 00000000000000000000000000000001
(32 bit)
$ bin = pack(“V”,1)=> 00000000000000000000000000000001(32位)
It tells pack how you want the data represented in the binary data. The code below will demonstrate this. Note that you can unpack with a different format from what you packed the data as.
它告诉pack你希望如何在二进制数据中表示数据。下面的代码将证明这一点。请注意,您可以使用与打包数据不同的格式解压缩。
<?php
$bin = pack("S", 65535);
$ray = unpack("S", $bin);
echo "UNSIGNED SHORT VAL = ", $ray[1], "\n";
$bin = pack("S", 65536);
$ray = unpack("S", $bin);
echo "OVERFLOW USHORT VAL = ", $ray[1], "\n";
$bin = pack("V", 65536);
$ray = unpack("V", $bin);
echo "SAME AS ABOVE BUT WITH ULONG VAL = ", $ray[1], "\n";
?>
#2
10
As noted in the php documentation for pack, the function is borrowed from Perl's pack function.
正如包的php文档中所述,该函数是从Perl的pack函数中借用的。
Take a look at Perl's documentation for pack, specifically the examples section at the very bottom of the page. PHP's pack does not implement all the formats, but Perl's documentation for the function does a better job of providing examples and explaining each format.
请查看Perl的包文档,特别是页面底部的示例部分。 PHP的包没有实现所有格式,但Perl的函数文档在提供示例和解释每种格式方面做得更好。
#1
21
Those represent how you want the data you are packing to be represented in binary format:
这些表示您希望包装的数据以二进制格式表示的方式:
so
所以
$bin = pack("v", 1); => 0000000000000001
(16bit)
$ bin = pack(“v”,1); => 0000000000000001(16位)
where
哪里
$bin = pack("V", 1) => 00000000000000000000000000000001
(32 bit)
$ bin = pack(“V”,1)=> 00000000000000000000000000000001(32位)
It tells pack how you want the data represented in the binary data. The code below will demonstrate this. Note that you can unpack with a different format from what you packed the data as.
它告诉pack你希望如何在二进制数据中表示数据。下面的代码将证明这一点。请注意,您可以使用与打包数据不同的格式解压缩。
<?php
$bin = pack("S", 65535);
$ray = unpack("S", $bin);
echo "UNSIGNED SHORT VAL = ", $ray[1], "\n";
$bin = pack("S", 65536);
$ray = unpack("S", $bin);
echo "OVERFLOW USHORT VAL = ", $ray[1], "\n";
$bin = pack("V", 65536);
$ray = unpack("V", $bin);
echo "SAME AS ABOVE BUT WITH ULONG VAL = ", $ray[1], "\n";
?>
#2
10
As noted in the php documentation for pack, the function is borrowed from Perl's pack function.
正如包的php文档中所述,该函数是从Perl的pack函数中借用的。
Take a look at Perl's documentation for pack, specifically the examples section at the very bottom of the page. PHP's pack does not implement all the formats, but Perl's documentation for the function does a better job of providing examples and explaining each format.
请查看Perl的包文档,特别是页面底部的示例部分。 PHP的包没有实现所有格式,但Perl的函数文档在提供示例和解释每种格式方面做得更好。