1
0
mirror of https://github.com/danog/PHPStruct.git synced 2024-11-30 04:19:08 +01:00
PHPStruct/README.md
2016-07-22 23:35:46 +02:00

2.6 KiB

PHPStruct class

Build Status Codacy Badge Packagist Packagist HHVM StyleCI

Licensed under MIT.

PHP implementation of Python's struct module.

This library was created to help me develop a client for the mtproto protocol.

The functions and the formats are exactly the ones used in python's struct (https://docs.python.org/3/library/struct.html)

This library can be used to pack/unpack strings, ints, floats, chars and bools into bytes. It has lots of advantages over PHP's native implementation of pack and unpack, such as:

  • Custom byte endianness.
  • Lots of useful formats that aren't present in the native implementation.
  • The syntax of the format string of pack and unpack is the same as in python's struct module.
  • The result of unpack is normal numerically indexed array that starts from 0 like it should.
  • The result of unpack has type casted values (int for integer formats, bool for boolean formats, float for float formats and string for all of the other formats).
  • Pack is more strict about the type of input formats.
  • The calcsize function is implemented.
  • The q and Q formats can be used even on 32 bit systems.

For now custom byte size may not work properly on certain machines for the i, I, f and d formats.

Installation

Install using composer:

composer require danog/phpstruct

Usage

require('vendor/autoload.php');
$struct = new \danog\PHP\Struct();
$pack = $struct->pack("2cxi", "ab", 44);
$unpack = $struct->unpack("2cxi", $pack);
var_dump($unpack);
$count = $struct->calcsize("2cxi");

This library can also be used statically:

require('vendor/autoload.php');
$pack = \danog\PHP\Struct::pack("2cxi", "ab", 44);
$unpack = \danog\PHP\Struct::unpack("2cxi", $pack);
var_dump($unpack);
$count = \danog\PHP\Struct::calcsize("2cxi");

Daniil Gentili