本文介绍了如何在PHP中将整数转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将整数转换为4个字节的数组?
how would I convert an integer to an array of 4 bytes?
这是我想移植的确切代码(在C#中)
Here is the exact code I want to port (in C#)
int i = 123456;
byte[] ar = BitConverter.GetBytes(i);
// ar will contain {64, 226, 1, 0}
我将如何在PHP中做完全相同的事情?
How would I do the exact same thing in PHP ?
推荐答案
等效的转换是
$i = 123456;
$ar = unpack("C*", pack("L", $i));
查看实际效果 .
See it in action.
您应该知道字节顺序(小/大字节序)取决于计算机体系结构(在BitConverter
情况下也是如此).那可能是好事,也可能不是好事.
You should be aware though that the byte order (little/big endian) is dependent on the machine architecture (as it is also in the case of BitConverter
). That might or might not be good.
这篇关于如何在PHP中将整数转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!