Working with binary data in PHP

I am writing a binary socket client in PHP and that is a pain. I am currently using packto convert numbers to binary strings, but this is not enough. Two options are possible pack:

  • Write signed 32-bit integer in machine byte order
  • Write 32-bit integer in bytes of large byte

But I need to write signed 32-bit integers in large trailing order, as in Java DataOutputStream.writeInteger. packdoes not have this option.

Is there a way to do this with pack, or convert the output pack, or perhaps the best binary data library in PHP?

+3
source share
2 answers

The PHP specification pack()states that unsigned / unsigned distinction only matters for unpack(), and not pack().

So just use the 32-bit network byte order ( N).

0
source

Are you using Zend Framework? If so, you can use the functionZend_Io_Writer::writeInt32BE()

Writes a signed 32-bit integer as binary data, ordered by billiant stream.

Or you should take a look at ZF-source how these guys deal with this.

+2
source

Source: https://habr.com/ru/post/1768052/


All Articles