Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with binary data in PHP

I'm writing a client for a binary socket protocol in PHP, and it's a pain. I'm currently using pack to convert numbers into binary strings, but it's lacking. Two options pack has are:

  • Write a signed 32 bit integer in machine byte order
  • Write an insigned 32 bit integer in big endian byte order

But I need to write signed 32 bit integers in big endian order, as with Java's DataOutputStream.writeInteger. pack doesn't have that option.

Is there a way to do this with pack, or to transform the output of pack, or maybe a better library for working with binary data in PHP?

like image 921
Bart van Heukelom Avatar asked Jan 27 '26 20:01

Bart van Heukelom


1 Answers

Do you use the Zend Framework? If so, you can use the function Zend_Io_Writer::writeInt32BE()

Writes signed 32-bit integer as big-endian ordered binary data to the stream.

Or else you should take a look at the ZF-source how these guys handle this.

like image 157
Bas van Dorst Avatar answered Jan 30 '26 09:01

Bas van Dorst



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!