由于php不支持uint32_t数据类型,因此当我通过TCP_Socket与服务器通信时出现问题。服务器正在将字节流作为unint32_t发送(这是c / c ++中的数据类型)。我尝试使用unpack解压缩这些字节,但它给出了负数或意外值。这是我的套接字通信:
$host = "127.0.0.1";
$port = 555;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");
// connect to server
socket_connect($socket, $host, $port) or die("Could not connect to server\n");
// read data from server
$result = socket_read ( $socket , 1024 ) or die("Could not read server response\n");
$tt= TcpSocket::_uint32be($result);
sprintf('%u', $tt);
echo " Test == ". $tt;
echo "1-replay from server :";
echo var_dump($tt);
这是我用来将字节转换为uint32_t到var。
的函数public static function _uint32be($bin)
{
// $bin is the binary 32-bit BE string that represents the integer
if (PHP_INT_SIZE <= 4){
list(,$h,$l) = unpack('n*', $bin);
return ($l + ($h*0x010000));
}
else{
list(,$int) = unpack('N', $bin);
return $int;
}
}
如何在php中解压缩或检索uint32_t?