如何在php中将字节数组转换为整数? [英] How to convert byte array to integer in php?
问题描述
我已经看到了一个代码,将整数转换为字节数组.以下是如何在php 3中将整数转换为字节数组的代码(
I have seen a code that converts integer into byte array. Below is the code on How to convert integer to byte array in php 3 (How to convert integer to byte array in php):
<?php
$i = 123456;
$ar = unpack("C*", pack("L", $i));
print_r($ar);
?>
上面的代码将输出:
//output:
Array
(
[1] => 64
[2] => 226
[3] => 1
[4] => 0
)
但是我现在的问题是如何逆向此过程.表示从 byte数组转换为整数.在上述情况下,输出为 123456
But my problem right now is how to reverse this process. Meaning converting from byte array into integer. In the case above, the output will be 123456
有人可以帮我这个忙吗?我会很大的帮助.谢谢你.
Can anybody help me with this. I would be a great help. Thanks ahead.
推荐答案
为什么不像对待数学问题一样对待它?
Why not treat it like the math problem it is?
$ i =($ ar [3]< 24)+($ ar [2]< 16)+($ ar [1]< 8)+ $ ar [0] ;
$i = ($ar[3]<<24) + ($ar[2]<<16) + ($ar[1]<<8) + $ar[0];
这篇关于如何在php中将字节数组转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!