PHP 字节 2 双字

发布于 2024-07-23 07:05:33 字数 234 浏览 3 评论 0原文

我有一个数组:

$arr[0] = 95
$arr[1] = 8
$arr[2] = 0
$arr[3] = 0

即字节。 我需要一个双字。 我尝试过:

$dword = $arr[0]+$arr[1]*265+$arr[2]*265*265+$arr[3]*265*265*265;

这是对的还是我做错了?

I have an array:

$arr[0] = 95
$arr[1] = 8
$arr[2] = 0
$arr[3] = 0

That are bytes. I need a DWORD.
I tried:

$dword = $arr[0]+$arr[1]*265+$arr[2]*265*265+$arr[3]*265*265*265;

Is that right or am I doing it wrong?

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

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

发布评论

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

评论(4

东京女 2024-07-30 07:05:34

尝试:

$dword = (($arr[3] & 0xFF) << 24) | (($arr[2] & 0xFF) << 16) | (($arr[1] & 0xFF) << 8) | ($arr[0] & 0xFF);

也可以按照您的方式进行一些更正:

$dword = $arr[0] + $arr[1]*0x100 + $arr[2]*0x10000 + $arr[3]*0x1000000;

或使用打包/解包:

$dword = array_shift(unpack("L", pack("CCCC", $arr[0], $arr[1], $arr[2], $arr[3])));

Try:

$dword = (($arr[3] & 0xFF) << 24) | (($arr[2] & 0xFF) << 16) | (($arr[1] & 0xFF) << 8) | ($arr[0] & 0xFF);

It can also be done your way with some corrections:

$dword = $arr[0] + $arr[1]*0x100 + $arr[2]*0x10000 + $arr[3]*0x1000000;

Or using pack/unpack:

$dword = array_shift(unpack("L", pack("CCCC", $arr[0], $arr[1], $arr[2], $arr[3])));
自此以后,行同陌路 2024-07-30 07:05:34

或者尝试

<?php
$arr = array(95,8,0,0);
$bindata = join('', array_map('chr', $arr));
var_dump(unpack('L', $bindata));

both (Emil H's and my code) give you 2143 as the result.

Or try

<?php
$arr = array(95,8,0,0);
$bindata = join('', array_map('chr', $arr));
var_dump(unpack('L', $bindata));

both (Emil H's and my code) give you 2143 as the result.

等你爱我 2024-07-30 07:05:34

或者至少使用 256 而不是 265。

Or at the very least use 256 rather than 265.

苏璃陌 2024-07-30 07:05:34

您的代码应该正确工作,但您应该乘以 256,而不是 265。(在 8 位中,有 2^8 = 256 个唯一值)。 它有效,因为乘以 256 与将位向左移动 8 位相同。

也许您应该考虑使用按位运算符来代替,以更好地传达意图。 请参阅http://theopensourcery.com/phplogic.htm

Your code should work correctly, but you should multiply with 256, not 265. (in 8 bits, there are 2^8 = 256 unique values). It works, because multiplying with 256 is the same as shifting the bits 8 places to the left.

Perhaps you should consider using the bitwise operators instead, to better convey the intent. See http://theopensourcery.com/phplogic.htm

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