2016-07-05 19:29:13 +02:00
# PHPStruct class
2016-07-04 01:43:23 +02:00
2016-07-05 20:57:15 +02:00
[![Build Status ](https://travis-ci.org/danog/PHPStruct.svg?branch=master )](https://travis-ci.org/danog/PHPStruct)
2016-07-15 12:50:26 +02:00
[![Codacy Badge ](https://api.codacy.com/project/badge/Grade/7b91e30ec89a4313bdb34766ea990113 )](https://www.codacy.com/app/daniil-gentili-dg/PHPStruct?utm_source=github.com& utm_medium=referral& utm_content=danog/PHPStruct& utm_campaign=Badge_Grade)
2016-07-29 16:56:09 +02:00
[![License ](https://img.shields.io/packagist/l/danog/phpstruct.svg?maxAge=2592000?style=flat-square )](https://opensource.org/licenses/MIT)
[![Packagist download count ](https://img.shields.io/packagist/dm/danog/phpstruct.svg?maxAge=2592000?style=flat-square )](https://packagist.org/packages/danog/phpstruct)
[![Packagist ](https://img.shields.io/packagist/v/danog/PHPStruct.svg?maxAge=2592000?style=flat-square )](https://packagist.org/packages/danog/phpstruct)
[![HHVM Status ](http://hhvm.h4cc.de/badge/danog/phpstruct.svg?style=flat-square )](http://hhvm.h4cc.de/package/danog/phpstruct)
2016-07-13 19:59:59 +02:00
[![StyleCI ](https://styleci.io/repos/62454134/shield )](https://styleci.io/repos/62454134)
2016-07-05 20:57:15 +02:00
2016-07-04 01:43:23 +02:00
Licensed under MIT.
2016-07-05 19:29:13 +02:00
PHP implementation of Python's struct module.
2016-07-04 01:43:23 +02:00
This library was created to help me develop a [client for the mtproto protocol ](https://github.com/danog/MadelineProto ).
2016-07-29 21:24:24 +02:00
It supports php 5.6, php 7 and HHVM.
2016-07-04 01:43:23 +02:00
2016-07-12 16:26:54 +02:00
The functions and the formats are exactly the ones used in python's struct
(https://docs.python.org/3/library/struct.html)
2016-07-04 01:43:23 +02:00
2016-07-22 23:35:46 +02:00
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.
2016-07-28 15:50:04 +02:00
* The q and Q formats can be used even on 32 bit systems (the downside is limited precision).
* Padding is supported for the @ modifier.
2016-07-22 23:35:46 +02:00
2016-07-26 16:23:31 +02:00
For now custom byte size may not work properly on certain machines for the f and d formats.
2016-07-04 01:43:23 +02:00
## Installation
Install using composer:
```
2016-07-05 19:29:13 +02:00
composer require danog/phpstruct
2016-07-04 01:43:23 +02:00
```
# Usage
2016-07-26 16:23:31 +02:00
Dynamic (recommended)
2016-07-04 01:43:23 +02:00
```
require('vendor/autoload.php');
2016-07-28 13:40:22 +02:00
$struct = new \danog\PHP\StructClass();
2016-07-09 21:12:33 +02:00
$pack = $struct->pack("2cxi", "ab", 44);
$unpack = $struct->unpack("2cxi", $pack);
var_dump($unpack);
$count = $struct->calcsize("2cxi");
2016-07-26 16:23:31 +02:00
```
Dynamic (while specifying format string during istantiation)
```
require('vendor/autoload.php');
2016-07-28 13:40:22 +02:00
$struct = new \danog\PHP\StructClass("2cxi");
2016-07-26 16:23:31 +02:00
$pack = $struct->pack("ab", 44);
$unpack = $struct->unpack($pack);
var_dump($unpack);
$count = $struct->size;
$formatstring = $struct->format;
2016-07-04 01:43:23 +02:00
```
2016-07-26 16:23:31 +02:00
Static
2016-07-09 21:12:33 +02:00
```
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");
```
2016-07-04 01:43:23 +02:00
[Daniil Gentili ](http://daniil.it )