1
0
mirror of https://github.com/danog/PHPStruct.git synced 2024-11-26 11:44:39 +01:00
Go to file
2016-09-28 02:45:06 +02:00
lib/danog/PHP Disable debugging 2016-09-28 02:45:06 +02:00
tests/danog/PHP Update badges, remove unused import in php2py.py and removed useless else in StructTest.php 2016-07-29 16:56:09 +02:00
.gitignore Fixed unpacking of signed negative numbers. 2016-07-25 12:40:16 +02:00
.styleci.yml Added styleci tests and badge 2016-07-13 19:59:59 +02:00
.travis.yml Trying to fix hhvm tests 2016-07-29 16:28:19 +02:00
composer.json Added some tags 2016-08-25 13:07:20 +02:00
composer.lock Fixed precision problem in decbin function 2016-08-14 19:51:41 +02:00
example.php Applied fixes from StyleCI 2016-08-14 12:42:47 -04:00
LICENSE First commit (master) 2016-07-04 01:43:23 +02:00
README.md Update README.md 2016-07-29 21:24:24 +02:00

PHPStruct class

Build Status Codacy Badge License Packagist download count Packagist HHVM Status 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.
It supports php 5.6, php 7 and HHVM.

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).
  • The calcsize function is implemented.
  • The q and Q formats can be used even on 32 bit systems (the downside is limited precision).
  • Padding is supported for the @ modifier.

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

Installation

Install using composer:

composer require danog/phpstruct

Usage

Dynamic (recommended)

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

Dynamic (while specifying format string during istantiation)

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

Static

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