https://github.com/danog/phpstruct
PHP implementation of Python's struct module.
https://github.com/danog/phpstruct
Last synced: over 1 year ago
JSON representation
PHP implementation of Python's struct module.
- Host: GitHub
- URL: https://github.com/danog/phpstruct
- Owner: danog
- License: mit
- Created: 2016-07-02T14:17:12.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2022-03-26T12:53:42.000Z (over 4 years ago)
- Last Synced: 2025-03-17T23:02:50.673Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 101 KB
- Stars: 11
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHPStruct class
[](https://travis-ci.org/danog/PHPStruct)
[](https://www.codacy.com/app/daniil-gentili-dg/PHPStruct?utm_source=github.com&utm_medium=referral&utm_content=danog/PHPStruct&utm_campaign=Badge_Grade)
[](https://opensource.org/licenses/MIT)
[](https://packagist.org/packages/danog/phpstruct)
[](https://packagist.org/packages/danog/phpstruct)
[](http://hhvm.h4cc.de/package/danog/phpstruct)
[](https://styleci.io/repos/62454134)
Licensed under MIT.
PHP implementation of Python's struct module.
This library was created to help me develop a [client for the mtproto protocol](https://github.com/danog/MadelineProto).
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](http://daniil.it)