代码之家  ›  专栏  ›  技术社区  ›  user2205930

PHP读取共享内存二进制数据(uint32 x=shmop_read(…))

  •  1
  • user2205930  · 技术社区  · 9 年前

    我有一个共享内存区域,希望通过运行在web服务器上的PHP访问该区域。共享内存的布局是,前4个字节是一个32位无符号整数,其中包含剩余共享内存区域内有效数据的字节数(数据可以是可变大小),即:

    Byte Range           Value
    ------------------   -----------------------------------------------
    0 - 3                32-bit unsigned integer - call it numBytes
    4 - (numBytes + 4)   char array - The actual data, total of numBytes
    

    如何将前四个字节作为整数读取?我唯一能想到的就是 shmop_read($key, 0, 4) 并将返回的字符串值转换为数组,然后按说明将该数组转换为整数 here 这一切看起来都很混乱,我想知道是否有一种更干净的方法可以做到这一点?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Jens A. Koch    9 年前

    我想这只是一个 unpack 具有 N V (取决于尾数)。

    所以,假设您的$numBytesString看起来像 \xff\xff\xff\x7f ? 那我会的 打开 :

    $numBytesString = shmop_read($key, 0, 4);
    
    $numBytesInt = unpack("V", $numBytesString); // assuming little-endianess
    
    // 2147483647