有人可以向我解释一下 PHP 中的 pack() 函数吗?

发布于 2024-07-24 09:24:03 字数 303 浏览 5 评论 0原文

我想了解有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妖妓 2024-07-31 09:24:03

这些表示您希望如何以二进制格式表示要打包的数据:

因此

$bin = pack("v", 1); => 0000000000000001(16位)

其中

$bin = pack("V", 1) => 00000000000000000000000000000001(32位)

它告诉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";
?>

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.

<?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";
?>
妞丶爷亲个 2024-07-31 09:24:03

正如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文