dongqiulei1987 2014-07-16 16:19
浏览 278
已采纳

pack('i',1)总是4个字节吗?

On my machine PHP_INT_SIZE is 8 but pack('i',1) returns a 4 byte string anyway.

The docs say i is a "signed integer (machine dependent size and byte order)" but that doesn't appear to be true. Is it fixed at 4 bytes?

This means that unpack('i',pack('i',PHP_INT_MAX))[1] !== PHP_INT_MAX on 64-bit machines because half the data is lopped off.

  • 写回答

2条回答 默认 最新

  • duanhuokuang5280 2014-07-16 18:17
    关注

    It's important to remember that PHP is written in C (in other words, it doesn't matter what PHP thinks an int is). Jumping off of the other answer about sizeof(int), it looks like it's a relic of 32 bit C compilers (emphasis mine)

    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.

    So, in short, it could be 2 bytes but only if you have a 16 bit processor. In 32 and 64 bit systems, C seems to say int is 4 bytes in all cases. So, yes, pack('i',1); will always be 4 bytes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用