19

In php is there a way to write binary data to the response stream,
like the equivalent of (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();



I would then like to be able read the response in a c# application, like

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();

4 Answers 4

15

In PHP, strings and byte arrays are one and the same. Use pack to create a byte array (string) that you can then write. Once I realized that, life got easier.

$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;
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for answer that helped me. I'm adding what worked for me below as another answer.
I prefer to use print() instead of echo because it's more like a function. Neither is technically a real function, though. The documentation calls them as "language constructs". It's also technically possible to do fopen("php://output") if you need file descriptor for output. For details, see php.net/manual/en/wrappers.php.php
@MikkoRantalainen You can call echo the same way. echo($my_byte_array); is perfectly valid, as is print $my_byte_array. They are the same. It's not necessarily a good idea to use parentheses for either one, though, since it's not actually a function. You're not actually calling print() with a parameter of $my_byte_array, you're "calling" print with a "parameter" of ($my_byte_array) (which is just an expression).
1

Usually, I use chr();

echo chr(255); // Returns one byte, value 0xFF

http://php.net/manual/en/function.chr.php

Comments

1

This is the same answer I posted to this, similar, question.

Assuming that array $binary is a previously constructed array bytes (like monochrome bitmap pixels in my case) that you want written to the disk in this exact order, the below code worked for me on an AMD 1055t running ubuntu server 10.04 LTS.

I iterated over every kind of answer I could find on the Net, checking the output (I used either shed or vi, like in this answer) to confirm the results.

<?php
$fp = fopen($base.".bin", "w");
$binout=Array();
for($idx=0; $idx < $stop; $idx=$idx+2 ){
    if( array_key_exists($idx,$binary) )
        fwrite($fp,pack( "n", $binary[$idx]<<8 | $binary[$idx+1]));
    else {
        echo "index $idx not found in array \$binary[], wtf?\n";
    }
}
fclose($fp);
echo "Filename $base.bin had ".filesize($base.".bin")." bytes written\n";
?>

Comments

0

You probably want the pack function -- it gives you a decent amount of control over how you want your values structured as well, i.e., 16 bits or 32 bits at a time, little-endian versus big-endian, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.