In PHP, I have some integer variables with values from 0-65535. I need to echo/print it directly, NOT as a printed sequence of characters like printing the string "1281" but the raw binary value. Also I need it so the binary integer sent to the output will always be exactly 2 bytes (zero bytes sent if needed so that it is always 2 bytes). How do I do this in php?
To elaborate on mu's answer, you want:
pack("S", $num));
E.g.:
file_put_contents("test65535.bin", pack("S", 65535));
file_put_contents("test100.bin", pack("S", 100));
file_put_contents("test0.bin", pack("S", 0));
hexdump test65535.bin
0000000 ffff
0000002
hexdump test100.bin
0000000 0064
0000002
hexdump test0.bin
0000000 0000
0000002
Yes it is possible, use pack.
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