PHP写二进制响应

god*_*ter 17 php binary response

在PHP中有一种方法可以将二进制数据写入响应流,
就像(c#asp)的等价物一样

System.IO.BinaryWriter Binary = new System.IO.BinaryWriter(Response.OutputStream);
Binary.Write((System.Int32)1);//01000000
Binary.Write((System.Int32)1020);//FC030000
Binary.Close();
Run Code Online (Sandbox Code Playgroud)



我希望能够在ac#应用程序中读取响应,就像

System.Net.HttpWebRequest Request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("URI");
System.IO.BinaryReader Binary = new System.IO.BinaryReader(Request.GetResponse().GetResponseStream());
System.Int32 i = Binary.ReadInt32();//1
i = Binary.ReadInt32();//1020
Binary.Close();
Run Code Online (Sandbox Code Playgroud)

And*_*nes 13

在PHP中,字符串和字节数组是同一个.使用pack创建一个字节数组(串),然后就可以写.一旦我意识到这一点,生活变得更容易.

$my_byte_array = pack("LL", 0x01000000, 0xFC030000);
$fp = fopen("somefile.txt", "w");
fwrite($fp, $my_byte_array);

// or just echo to stdout
echo $my_byte_array;
Run Code Online (Sandbox Code Playgroud)