PHPackages                             erickmajor/binary-parser - 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. erickmajor/binary-parser

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

erickmajor/binary-parser
========================

Binary Parser is a library that serializes and unserializes binary data in PHP.

v0.0.2(6y ago)19MITPHPPHP &gt;=5.6.0

Since May 19Pushed 5y agoCompare

[ Source](https://github.com/erickmajor/binary-parser)[ Packagist](https://packagist.org/packages/erickmajor/binary-parser)[ Fund](https://ko-fi.com/erickmajor)[ RSS](/packages/erickmajor-binary-parser/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

Binary Parser
=============

[](#binary-parser)

Serializes and unserializes binary data in PHP.

Constants
---------

[](#constants)

- NOT\_A\_NUMBER: Defines a kind of float error-flag, which isn't a number at all
- POSITIVE\_INFINITY: Defines a positive number higher than the maximum allowed
- NEGATIVE\_INFINITY: Defines a negative number lower than the minimum allowed

Classes
-------

[](#classes)

### BinaryParser: The binary encoder/decoder

[](#binaryparser-the-binary-encoderdecoder)

#### Properties

[](#properties)

- public bigEndian: bool = false =&gt; specify which byte order the class will use the decode/encode
- public constructor( bigEndian: bool = false ) =&gt; it may receive the endian kind

#### Methods

[](#methods)

- public function toFloat( data: string ): float =&gt; receives string containing 4 chars/bytes and returns its respective float representation or a special value (NOT\_A\_NUMBER, POSITIVE\_INFINITY, NEGATIVE\_INFINITY)
- public function fromFloat( data: float ): string =&gt; receives a floating point number and returns its binary representation in a 4 chars string, it may rise an exceptions if the number is a special value (NOT\_A\_NUMBER, POSITIVE\_INFINITY, NEGATIVE\_INFINITY) or if the number can't be represented (overflow, underflow)
- public function toDouble( data: string ): float =&gt; receives string containing 8 chars/bytes and returns its respective double representation or a special value (NOT\_A\_NUMBER, POSITIVE\_INFINITY, NEGATIVE\_INFINITY)
- public function fromDouble( data: float ): string =&gt; receives a floating point number and returns its binary representation in a 8 chars string, it may rise an exceptions if the number is a special value (NOT\_A\_NUMBER, POSITIVE\_INFINITY, NEGATIVE\_INFINITY) or if the number can't be represented (overflow, underflow)
- public function toSmall( data: string ): int =&gt; receives string containing 1 char/byte and returns your respective numeric representation with signal
- public function fromSmall( data: int ): string =&gt; receives a number and returns its binary representation in a 1 char string, it may rise an exception if the number exceeds the limit
- public function toByte( data: string ): int =&gt; receives string containing 1 char/byte and returns its respective numeric representation without signal
- public function fromByte( data: int ): string =&gt; receives a number and returns its binary representation in a 1 char string, it may rise an exception if the number exceeds the limit and negative numbers are converted
- public function toShort( data: string ): int =&gt; receives string containing 2 chars/bytes and returns its respective numeric representation with signal
- public function fromShort( data: int ): string =&gt; receives a number and returns its binary representation in a 2 chars string, it may rise an exception if the number exceeds the limit
- public function toWord( data: string ): int =&gt; receives string containing 2 chars/bytes and returns its respective numeric representation without signal
- public function fromWord( data: int ): string =&gt; receives a number and returns its binary representation in a 2 chars string, it may rise an exception if the number exceeds the limit and negative numbers are converted
- public function toInt( data: string ): int =&gt; receives string containing 4 chars/bytes and returns its respective numeric representation with signal
- public function fromInt( data: int ): string =&gt; receives a number and returns its binary representation in a 4 chars string, it may rise an exception if the number exceeds the limit
- public function toDWord( data: string ): int =&gt; receives string containing 4 chars/bytes and returns its respective numeric representation without signal
- public function fromDWord( data: int ): string =&gt; receives a number and returns its binary representation in a 4 chars string, it may rise an exception if the number exceeds the limit and negative numbers are converted
- protected function decodeFloat( data: string, precisionBits: int, exponentBits: int ): float =&gt; internal function for decoding the standard IEEE-754, since PHP isn't able to handle float types using more than 64bits, I decided to not support the 80 and 128 bits formats.
    data: a string buffer containing the binary representation of the number (must have at least "ceil( ( exponentBits + precisionBits + 1 ) / 8 )" bytes)
    precisionBits: the amount of bits that specify the precision/significand
    exponentBits: the amount of bits that specify the exponent that will multiply the significand to obtain the number and returns the number or the following special values: NaN, +Infinity, -Infinity
- protected function decodeInt( data: string, bits: int, signed: bool ): int =&gt; internal function for decoding standard integer types, since PHP isn't able to handle integers higher than 32 bits, i decided to not support the 64 bits format.
    data: a string buffer containing the binary representation of the number (must have at least "ceil( bits / 8 )" bytes) bits: the amount of bits that specify the number max length and min length signed: if the number must be decoded as signed or unsigned and returns the number
- protected function encodeFloat( data: float, precisionBits: int, exponentBits: int ): string =&gt; internal function for encoding a number into the standard IEEE-754 format, since PHP isn't able to handle float types using more than 64bits, i decided to not support the 80 and 128 bits formats.
    data: the number which will be converted
    precisionBits: the amount of bits that specify the precision/significand
    exponentBits: the amount of bits that specify the exponent that will multiply the significand to obtain the number and returns a binary string representation of the number containing "ceil( ( exponentBits + precisionBits + 1 ) / 8 )" bytes
- protected function encodeInt( data: string, bits: int, signed: bool ): int =&gt; internal function for decoding standard integer types, since PHP isn't able to handle integers higher than 32 bits, i decided to not support the 64 bits format.
    data: the number which will be converted
    bits: the amount of bits that specify the number max length and min length
    signed: if the number must be decoded as signed or unsigned and returns a binary string representation of the number containing "ceil( bits / 8 )" bytes

### BinaryBuffer

[](#binarybuffer)

Simple class to hold some bytes while decoding the data into a numerical format

#### Properties

[](#properties-1)

- public length: int = 0 =&gt; the amount of bytes stored in the class
- private bigEndian: bool = false =&gt; keeps safe the internal endian-format that was chosed
- private buffer: array = \[\] =&gt; buffer storage, hold the bytes

#### Methods

[](#methods-1)

- public constructor( bigEndian: bool = false, buffer: string = '' ): void =&gt; it may receive the ordering type and a starting string buffer
- public function setBuffer( data: string ): void =&gt; set new content into the buffer array
- public function getBuffer(): array =&gt; returns the buffer array
- public function hasNeededBits( neededBits: int ): bool =&gt; returns if the buffer has "neededBits" bits avaiable for reading
- public function checkBuffer( $neededBits ): void =&gt; the same as BinaryBuffer::hasNeededBits, but it rises an exception when there isn't enough data available
- public function byte( $i ): int =&gt; returns the byte value in the buffer at the specified "i" offset
- public function setEndian( $bigEndian ): void =&gt; sets up the endian
- public function readBits( $start, $length ): int =&gt; read the bits interval and returns the corresponding integer value
- private private function shl( a: Int, b: Int ): int =&gt; rotates "a" bits "b" times to the left

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.3% 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 ~0 days

Total

2

Last Release

2190d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c379e9a7f7404438a9eca793b4ad0e60d8a6e0d046a6fde722dc765c9a851286?d=identicon)[erick.major](/maintainers/erick.major)

---

Top Contributors

[![erickmajor](https://avatars.githubusercontent.com/u/5443488?v=4)](https://github.com/erickmajor "erickmajor (14 commits)")[![jonasraoni](https://avatars.githubusercontent.com/u/361921?v=4)](https://github.com/jonasraoni "jonasraoni (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/erickmajor-binary-parser/health.svg)

```
[![Health](https://phpackages.com/badges/erickmajor-binary-parser/health.svg)](https://phpackages.com/packages/erickmajor-binary-parser)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[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)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

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

PHPackages © 2026

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