I'm trying to send the binary representation of data through a PHP socket.
So if the number was 15, socket_write would send 00001111 as binary representation for 15.
How should I do this?
Use the chr function:
socket_write( $fp, chr(15));
You can also use \x escapes with hex values:
socket_write( $fp, "\xff\xff\xff\xff" );
Would send 4 bytes, with values 255 on each.
To generate a binary representation from arbitrary data use the pack() function.
Example:
$data = pack("v", 15); // "i" is unsigned little endian integer
socket_write($socket, $data);
See the manual page for more information on the various formats. There's also unpack() for the reverse functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With