PHP 包包含多个不同长度的值

发布于 2025-01-14 07:01:04 字数 379 浏览 3 评论 0原文

我想打包一个由 64 位、32 位和 32 位整数组成的字符串。 我对 pack 函数(或全部)没有太多经验,所以我尝试做这样的事情:

pack('JNN', 1, 2, 3);
// and
unpack('JNN');

但这并没有产生我想要的结果。

问题是,当我运行该代码时,我收到以下信息:

array [
  "NN" => 1
]

但我期望这样:

array [
  1,
  2,
  3
]

知道如何解决这个问题吗?

提前致谢,

I'd like to pack a string consisting of a 64 bits, 32 bits and 32 bits ints.
I don't have much experience with the pack function (or bits altogether) so I'm trying to do something like this:

pack('JNN', 1, 2, 3);
// and
unpack('JNN');

But that does not yield the result I'm after.

The problem is that when I run that code I receive the following:

array [
  "NN" => 1
]

But I expected this:

array [
  1,
  2,
  3
]

Any idea how to approach this?

Thanks in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小嗲 2025-01-21 07:01:04

pack 创建一个 16 个字符的字符串。

$str = pack('JNN', 1, 2, 3);
//string(16) "\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03"

unpack 需要格式元素的键。示例:

$arr = unpack('Jint64/N2int32_',$str);
/*
array (
  'int64' => 1,
  'int32_1' => 2,
  'int32_2' => 3,
)
*/

有关更多示例,请参阅 PHP 手册中的 Unpack

如果需要纯数字键,可以使用 array_values 函数。

pack creates a 16-character string.

$str = pack('JNN', 1, 2, 3);
//string(16) "\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03"

unpack requires keys for the format elements. Example:

$arr = unpack('Jint64/N2int32_',$str);
/*
array (
  'int64' => 1,
  'int32_1' => 2,
  'int32_2' => 3,
)
*/

For more examples, see Unpack in the PHP manual.

If purely numeric keys are required, the array_values function can be used.

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