有人可以向我解释一下 PHP 中的 pack() 函数吗?
我想了解有关 PHP 中 pack()
函数的更多信息: https://secure.php.net/manual/en/function.pack.php
我知道它将数据打包成二进制,但我不确定所有这些 v V n N c C
的意思是,我想知道是否有人可以给我一个实际的演示何时使用哪些格式?
在我看来,在线文档有所改变,缺乏信息。
I would like to know more about the pack()
function in PHP: 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?
The online documentation, for a change, lacks of information, in my opinion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些表示您希望如何以二进制格式表示要打包的数据:
因此
$bin = pack("v", 1); => 0000000000000001
(16位)其中
$bin = pack("V", 1) => 00000000000000000000000000000001
(32位)它告诉pack您希望如何以二进制数据表示数据。
下面的代码将演示这一点。 请注意,您可以使用不同的解包
格式来自您打包数据的格式。
Those represent how you want the data you are packing to be represented in binary format:
so
$bin = pack("v", 1); => 0000000000000001
(16bit)where
$bin = pack("V", 1) => 00000000000000000000000000000001
(32 bit)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 文档中所述,该函数借用自 Perl 的 pack 函数。
查看 Perl 的 pack 文档,特别是位于最底部的示例部分页。 PHP 的包并未实现所有格式,但 Perl 的函数文档在提供示例和解释每种格式方面做得更好。
As noted in the php documentation for pack, the function is borrowed from Perl's pack function.
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.