PHP 字节 2 双字
我有一个数组:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
也可以按照您的方式进行一些更正:
或使用打包/解包:
Try:
It can also be done your way with some corrections:
Or using pack/unpack:
或者尝试
both (Emil H's and my code) give you 2143 as the result.
Or try
both (Emil H's and my code) give you 2143 as the result.
或者至少使用 256 而不是 265。
Or at the very least use 256 rather than 265.
您的代码应该正确工作,但您应该乘以 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