pigeonboys/php-binary-parser
Composer 安装命令:
composer require pigeonboys/php-binary-parser
包简介
A PHP library for parsing binary data, with full support for signed/unsigned integers and endian-aware formats.
README 文档
README
A lightweight PHP library for parsing binary data from byte arrays. Supports signed and unsigned integers, arrays, and endian-aware formats (big/little endian). Ideal for reading hardware data streams, binary protocols, or any low-level byte-oriented data.
Features
- Supports 8, 16, 24, 32, and 64-bit integers
- Signed and unsigned parsing
- Little-endian and big-endian support
- Bytes are consumed from the buffer after each read
- Multi-read support (
readMany()) - Resettable buffer with
resetBuffer() - Type-safe with meaningful exceptions
Installation
You can install via Composer:
composer require pigeonboys/php-binary-parser
PHP version 8.0+ is required.
Usage
Basic Usage
use PigeonBoys\PhpBinaryParser\BinaryParser; $buffer = [0x12, 0x34, 0x56, 0x78]; $parser = new BinaryParser($buffer); // Read an unsigned 16-bit integer in big-endian $value = $parser->int16()->unsigned()->bigEndian()->read(); echo $value; // 4660 // After this read, the first two bytes are removed from the buffer echo $parser->bytesRemaining(); // 2
Reading Multiple Values
$parser = new BinaryParser([0x01, 0x02, 0x03, 0x04]); // Read two unsigned 8-bit integers $values = $parser->int8()->unsigned()->readMany(2); print_r($values); // [1, 2] // Only remaining bytes are left in the buffer echo $parser->bytesRemaining(); // 2
Resetting Buffer
$parser->resetBuffer(); // restores all original bytes
Methods
int8(),int16(),int24(),int32(),int64()— Set the bitnesssigned(),unsigned()— Set the signednesslittleEndian(),bigEndian()— Set the endiannessread()— Read a single value and consume bytes from the bufferreadMany(int $count)— Read multiple values and consume bytes from the bufferresetBuffer()— Reset buffer to original contentbytesRemaining()— Get the number of bytes left
Exceptions
BitnessAlreadySetException— Thrown if you try to set the bitness more than once before reading.EndiannessAlreadySetException— Thrown if you try to set the endianness more than once before reading.SignednessAlreadySetException— Thrown if you try to set signedness more than once before reading.BitnessNotSetException— Thrown if you attempt to read an integer without first specifying the bitness.EndiannessNotSetException— Thrown if you attempt to read a multi-byte integer without specifying endianness (except 8-bit).SignednessNotSetException— Thrown if you attempt to read an integer without specifying signed or unsigned.InvalidLengthException— Thrown if the number of bytes requested forreadBytes()is less than 1.InvalidCountException— Thrown ifreadMany()is called with a count less than 1.CurrentBufferUnderflowException— Thrown if there are not enough bytes left in the buffer to satisfy a read.
pigeonboys/php-binary-parser 适用场景与选型建议
pigeonboys/php-binary-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pigeonboys/php-binary-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pigeonboys/php-binary-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-02