pburggraf/binary-utils

v0.5.0 2020-04-10 16:11 UTC

This package is auto-updated.

Last update: 2024-11-29 04:41:29 UTC


README

Class for working with binary data in PHP >=7.2

How to use

<?php

require __DIR__ . '/vendor/autoload.php';

use \PBurggraf\BinaryUtilities\BinaryUtilityFactory;
use \PBurggraf\BinaryUtilities\DataType\Byte;

file_put_contents('/tmp/temp.txt', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');

$binaryUtility = BinaryUtilityFactory::create();

// Read data
$result = $binaryUtility
    ->setFile('/tmp/temp.txt')
    ->setOffset(0x08)
    ->readArray(Byte::class, 4)
    ->returnBuffer();

var_dump($result);
// Expected result:
// array(4) {
//     [0] =>
//   string(2) "56"
//     [1] =>
//   string(2) "57"
//     [2] =>
//   string(2) "65"
//     [3] =>
//   string(2) "66"
// }

// Write data
$binaryUtility
    ->setOffset(0x08)
    ->writeArray(Byte::class, [66, 65, 57, 56])
    ->save();

var_dump(file_get_contents('/tmp/temp.txt'));
// Expected result:
// string(36) "01234567BA98CDEFGHIJKLMNOPQRSTUVWXYZ"