在转换一个PHP插座检索字节到整数 [英] Converting bytes retrieved over a php socket to a integer

查看:187
本文介绍了在转换一个PHP插座检索字节到整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是网状API创建如果客户端发送PROGRAMM任何服务器像5.一些它与4字节的整数,发送Java服务器。我觉得应该是像写一个整数到一个OutputStream相同。所以我想现在的PHP收到整数。但如果我打电话socket_read它输出任何有益的,因为它不的4个字节转换成整数。它只是使用这个文本。

I am using the netty API to create a Java server which sends if a client programm sends anything to the server a number like 5. It is a integer with 4 bytes. I think it should be the same like writing an integer into an OutputStream. So I want to receive the integer now by php. But if I call socket_read it outputs nothing helpfull because it does not convert the 4 bytes into a integer. It just uses this as text.

如果我把它工作这么一个char我需要一种方法在PHP将其转换。

If I send a char it works so I need a way in php to convert it.

还是会是最好的所有转换为文本客户方,所以全部是基于文本的。
我想,也许我必须坚持一个基于文本或二进制输出。

Or would it be the best to convert all into text clientside, so all is text-based. I think maybe I have to stick with a text-based or binary output.

编辑:与此相关的另一个问题是,让我们说我有在java中4个字节。我怎样才能建立一个整数?我必须Concat的4个字节?

Another question related to this is, let's say I have 4 bytes in java. How can I build a Integer? Do I have to concat the 4 bytes?

例如:

字节1:00000000

Byte 1: 00000000

字节2:00010000

Byte 2: 00010000

字节3:00000101

Byte 3: 00000101

字节4:00000001

Byte 4: 00000001

结果:00000000 00010000 00000101 00000001

Result: 00000000 00010000 00000101 00000001

请问这是正确的?

推荐答案

您可能需要使用的解压功能。是这样的:

You probably want to use the unpack function. Something like:

$data = socket_read($sock,4);
$x = unpack("N",$data);

这篇关于在转换一个PHP插座检索字节到整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆