PHPackages                             zeran/byte-buffer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. zeran/byte-buffer

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

zeran/byte-buffer
=================

Php library to parse and build byte buffer

2.0(1y ago)01GPL-3.0-or-laterPHPPHP &gt;=8.3

Since Apr 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/tsiura/byte-buffer)[ Packagist](https://packagist.org/packages/zeran/byte-buffer)[ RSS](/packages/zeran-byte-buffer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (6)Used By (0)

Zeran ByteBuffer
================

[](#zeran-bytebuffer)

A PHP library for binary data manipulation with support for multiple buffer implementations.

Introduction
------------

[](#introduction)

Zeran ByteBuffer is a powerful library for manipulating binary data in PHP. It provides a consistent interface for working with byte arrays and binary strings, supporting operations like reading and writing integers, floats, doubles, and strings in both little-endian and big-endian formats.

The library includes two primary implementations:

- `ArrayBuffer`: Stores data as an array of integers
- `StringBuffer`: Stores data as a string, which can be more memory-efficient

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require zeran/byte-buffer
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

```
use Zeran\ByteBuffer\ArrayBuffer;
use Zeran\ByteBuffer\StringBuffer;

// Choose an implementation
$buffer = new ArrayBuffer();
// or
$buffer = new StringBuffer();

// Write data
$buffer->writeInt16(1000);   // Write signed 16-bit integer
$buffer->writeUInt32(50000); // Write unsigned 32-bit integer
$buffer->writeFloat(123.45); // Write float
$buffer->writeUtf8String("Hello World"); // Write string

// Read data (remember to manage your position)
$buffer->setPosition(0);
$int16 = $buffer->readInt16();   // Read signed 16-bit integer
$uint32 = $buffer->readUInt32(); // Read unsigned 32-bit integer
$float = $buffer->readFloat();   // Read float
$string = $buffer->readUtf8String(11); // Read 11 bytes as UTF-8 string
```

### Creating Buffers

[](#creating-buffers)

```
// Create empty buffer
$buffer = new ArrayBuffer();

// Create from byte array
$buffer = ArrayBuffer::fromArray([0x01, 0x02, 0x03]);

// Create from string
$buffer = StringBuffer::fromString("Hello");

// Alternatively, write to buffer
$buffer = new StringBuffer();
$buffer->writeUtf8String("Hello");
```

### Integers (Signed and Unsigned)

[](#integers-signed-and-unsigned)

```
// Write integers in different sizes and endianness
$buffer->writeInt8(-50);          // 1-byte signed
$buffer->writeUInt8(100);         // 1-byte unsigned
$buffer->writeInt16(1000);        // 2-byte signed (little-endian)
$buffer->writeInt16BE(1000);      // 2-byte signed (big-endian)
$buffer->writeUInt16(1000);       // 2-byte unsigned (little-endian)
$buffer->writeUInt16BE(1000);     // 2-byte unsigned (big-endian)
$buffer->writeInt32(-123456);     // 4-byte signed (little-endian)
$buffer->writeUInt32BE(123456);   // 4-byte unsigned (big-endian)
$buffer->writeInt64(-9876543210); // 8-byte signed (little-endian)

// Generic write methods with custom size
$buffer->writeInt(-1000, 3);     // 3-byte signed (little-endian)
$buffer->writeUIntBE(100000, 3); // 3-byte unsigned (big-endian)

// Reading integers
$buffer->setPosition(0);
$int8 = $buffer->readInt8();
$uint8 = $buffer->readUInt8();
$int16 = $buffer->readInt16();
$int16be = $buffer->readInt16BE();
// ...and so on
```

### Floating-point numbers

[](#floating-point-numbers)

```
// Write float (4 bytes)
$buffer->writeFloat(100.5);
$buffer->writeFloatBE(100.5); // Big-endian

// Write double (8 bytes)
$buffer->writeDouble(123.456);
$buffer->writeDoubleBE(123.456); // Big-endian

// Reading floating-point numbers
$buffer->setPosition(0);
$float = $buffer->readFloat();
$floatBE = $buffer->readFloatBE();
$double = $buffer->readDouble();
$doubleBE = $buffer->readDoubleBE();
```

### Strings and Bytes

[](#strings-and-bytes)

```
// Write string
$buffer->writeUtf8String("Hello, world!");

// Write raw bytes
$buffer->writeBytes([0x0A, 0x0B, 0x0C, 0x01, 0x02, 0x03]);

// Reading strings and bytes
$buffer->setPosition(0);
$string = $buffer->readUtf8String(13); // Specify length or leave null to read all remaining
$bytes = $buffer->readBytes(6);        // Read 6 bytes as array
```

### Arrays/Lists

[](#arrayslists)

```
// Write arrays of integers
$buffer->writeListUInt8([1, 2, 3, 4, 5]);
$buffer->writeListUInt16([1000, 2000, 3000]);
$buffer->writeListUInt32([100000, 200000, 300000]);

// Alternative syntax
$buffer->writeList8([1, 2, 3, 4, 5]);
$buffer->writeList16([1000, 2000, 3000]);

// Reading arrays/lists
$buffer->setPosition(0);
$uint8List = $buffer->readListUInt8(5);    // Read 5 uint8 values
$uint16List = $buffer->readListUInt16(3);  // Read 3 uint16 values
$uint32List = $buffer->readListUInt32(3);  // Read 3 uint32 values
```

### Buffer Management

[](#buffer-management)

```
// Get current position
$position = $buffer->getPosition();

// Set position
$buffer->setPosition(4);

// Get total buffer size
$size = $buffer->size();

// Check remaining bytes
$remaining = $buffer->remains();

// Check if there are more bytes to read
$hasMore = $buffer->isMore();

// Trim the buffer
$buffer->ltrim(); // Remove bytes before current position
$buffer->rtrim(); // Remove bytes after current position

// Clear the buffer
$buffer->clear();

// Get raw buffer
$rawBuffer = $buffer->getBuffer(); // Returns string

// Convert buffer to byte array
$byteArray = $buffer->toBytesArray();

// String representation (hex format)
$hexString = $buffer->__toString(); // e.g., "01 02 03 04"

// Export as string
$encodedString = $buffer->encode();
```

Type Range Limitations
----------------------

[](#type-range-limitations)

The library automatically validates that values fit within their specified type ranges:

TypeSize (bytes)RangeInt81-128 to 127UInt810 to 255Int162-32,768 to 32,767UInt1620 to 65,535Int243-8,388,608 to 8,388,607UInt2430 to 16,777,215Int324-2,147,483,648 to 2,147,483,647UInt3240 to 4,294,967,295Int648-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807UInt6480 to 18,446,744,073,709,551,615Endianness
----------

[](#endianness)

Most methods come in two variants:

- Standard (e.g., `writeInt32`): Uses little-endian (least significant byte first)
- BE suffix (e.g., `writeInt32BE`): Uses big-endian (most significant byte first)

Which Implementation to Use?
----------------------------

[](#which-implementation-to-use)

- `ArrayBuffer`: Better for cases where you need to manipulate individual bytes frequently
- `StringBuffer`: More memory-efficient for larger buffers when you primarily write and read sequentially

Both implementations share the same interface (`BufferInterface`), so your code can switch between them without changes.

Testing
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance46

Moderate activity, may be stable

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~181 days

Total

5

Last Release

413d ago

Major Versions

1.3 → 2.02025-04-01

PHP version history (2 changes)1.0PHP ^8.1

1.2PHP &gt;=8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/1f2f8eceb33bed0c09d174955067087ad583f698a05e79b998224c4a35a8c58a?d=identicon)[zeran](/maintainers/zeran)

---

Top Contributors

[![tsiura](https://avatars.githubusercontent.com/u/1318413?v=4)](https://github.com/tsiura "tsiura (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/zeran-byte-buffer/health.svg)

```
[![Health](https://phpackages.com/badges/zeran-byte-buffer/health.svg)](https://phpackages.com/packages/zeran-byte-buffer)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[meyfa/php-svg

Read, edit, write, and render SVG files with PHP

54613.9M42](/packages/meyfa-php-svg)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
