gpt4 book ai didi

php - 包 ('i' ,1) 总是 4 个字节吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:38:30 27 4
gpt4 key购买 nike

在我的机器上 PHP_INT_SIZE8pack('i',1) 无论如何都会返回一个 4 字节的字符串。

The docsi 是一个“有符号整数(取决于机器的大小和字节顺序)”,但这似乎不是真的。是否固定为4字节?

这意味着在 64 位机器上 unpack('i',pack('i',PHP_INT_MAX))[1] !== PHP_INT_MAX 因为一半的数据被砍掉了。

最佳答案

重要的是要记住 PHP is written in C (换句话说,PHP 认为 int 是什么并不重要)。跳出关于 sizeof(int) 的另一个答案,它看起来像是 32 位的遗物 C compilers (强调我的)

Yes, it depends on both processors (more specifically, ISA, instruction set architecture, e.g., x86 and x86-64) and compilers including programming model. For example, in 16-bit machines, sizeof (int) was 2 bytes. 32-bit machines have 4 bytes for int. It has been considered int was the native size of a processor, i.e., the size of register. However, 32-bit computers were so popular, and huge number of software has been written for 32-bit programming model. So, it would be very confusing if 64-bit computer would have 8 bytes for int. Both Linux and Windows remain 4 bytes for int. But, they differ in the size of long.

所以,简而言之,它可以是 2 个字节,但前提是你有一个 16 位处理器。在 32 位和 64 位系统中,C 似乎在所有情况下都说 int 是 4 个字节。所以,是的,pack('i',1); 将始终为 4 个字节。

关于php - 包 ('i' ,1) 总是 4 个字节吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24785801/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com