PHPackages                             thesis/varint - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. thesis/varint

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

thesis/varint
=============

Varint and ZigZag integer encoding.

0.2.0(5mo ago)01.9k1MITPHPPHP ^8.4CI failing

Since Nov 22Pushed 5mo agoCompare

[ Source](https://github.com/thesis-php/varint)[ Packagist](https://packagist.org/packages/thesis/varint)[ Fund](https://www.tinkoff.ru/cf/5MqZQas2dk7)[ RSS](/packages/thesis-varint/feed)WikiDiscussions 0.2.x Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (8)Used By (1)

Varint and ZigZag encoding implementation
=========================================

[](#varint-and-zigzag-encoding-implementation)

[Variable-width integers](https://protobuf.dev/programming-guides/encoding/#varints) are at the core of the wire format of protobuf, kafka protocol and many other protocols and codecs.

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

[](#installation)

```
composer require thesis/varint
```

Usage
-----

[](#usage)

The library uses `\BcMath\Number` instead of `int` in its API to support the serialization of large numbers and avoid overflow issues. A library built on top of `thesis/varint` may choose to offer `int` in its API if it's certain that no overflow issues will occur or handle overflow errors itself. This is not the responsibility of *this* library.

Example of **varint** encoding using `BcMath` implementation:

```
use Thesis\Varint;
use BcMath\Number;

$codec = Varint\BcMath::Codec;

$buffer = $codec->encodeVarint(new Number('125'));
echo $codec->decodeVarint($buffer)->value; // '125'
```

[Zigzag](https://lemire.me/blog/2022/11/25/making-all-your-integers-positive-with-zigzag-encoding/) encoding is used for serializing negative varint numbers.

```
use Thesis\Varint;
use BcMath\Number;

$codec = Varint\BcMath::Codec;

$buffer = $codec->encodeVarint($codec->encodeZigZag(new Number('-125')));
echo $codec->decodeZigZag($codec->decodeVarint($buffer))->value; // '-125'
```

You can get the size of a varint in bytes before encoding it:

```
use Thesis\Varint;
use BcMath\Number;

$codec = Varint\BcMath::Codec;

echo $codec->size(new Number('128')); // 2
```

Likewise, you can decode a varint to get both the number and its size in bytes:

```
use Thesis\Varint;
use BcMath\Number;

$codec = Varint\BcMath::Codec;

$sized = $codec->decodeVarintSized($codec->encodeVarint(new Number('128')));
echo $sized->value; // '128'
echo $sized->size; // 2
```

This is useful to use in protocols because you don't know in advance how many bytes to consume from the buffer to read a varint. Therefore, you read it first and then consume that many bytes from the buffer based on the size obtained from `$sized->size`.

Why not use `brick/math`?
-------------------------

[](#why-not-use-brickmath)

`brick/math` is a fairly good library, and an early version of this library used it specifically. However, while developing the protobuf tooling, it became clear that working with `numeric-string` was inconvenient, so a type that was simple and stable enough to be part of the API was needed. Here, the `BigInteger` from `brick/math` faces a competitor in the form of the `Number` type from `bcmath` for PHP 8.4.

Since `bcmath` is a [bundled extension](https://www.php.net/manual/en/extensions.membership.php#extensions.membership.bundled), users won't need to install it explicitly, making it a more stable dependency compared to `brick/math`, which still hasn't reached a major version. Although `brick/math` allows using the `gmp` extension instead of `bcmath` as a faster alternative, this speed is lost behind `brick/math`'s abstractions, as a simple benchmark of varint/zigzag encoding demonstrated.

Therefore, we decided to settle on `bcmath` and explicitly use its `Number` type.

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance70

Regular maintenance activity

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 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 ~1 days

Total

5

Last Release

175d ago

PHP version history (2 changes)0.1.0PHP ^8.3

0.1.x-devPHP ^8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2552865?v=4)[Valentin Udaltsov](/maintainers/vudaltsov)[@vudaltsov](https://github.com/vudaltsov)

---

Top Contributors

[![kafkiansky](https://avatars.githubusercontent.com/u/37590388?v=4)](https://github.com/kafkiansky "kafkiansky (13 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/thesis-varint/health.svg)

```
[![Health](https://phpackages.com/badges/thesis-varint/health.svg)](https://phpackages.com/packages/thesis-varint)
```

###  Alternatives

[agencetwogether/hookshelper

Simple plugin to toggle display hooks available in current page.

2312.7k](/packages/agencetwogether-hookshelper)[qbhy/hyperf-multi-env

287.4k2](/packages/qbhy-hyperf-multi-env)[speixoto/yii2-amcharts

AmCharts Widget for Yii 2

1414.2k](/packages/speixoto-yii2-amcharts)

PHPackages © 2026

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