LEB Base-128 Integer Utility

LEB128 Encoder & Decoder

Use this LEB128 Encoder & Decoder to convert decimal integers to ULEB128 or SLEB128 hexadecimal bytes and decode Little Endian Base 128 byte sequences back to exact 32-bit or 64-bit integer values.

✓ ULEB128 ✓ SLEB128 ✓ Hex Decoder ✓ Canonical Check ✓ 64-Bit BigInt
128
Encode / Decode LEB128
● Ready
ULEB128 handles non-negative integers. SLEB128 preserves signed values.
Used for input and decoded-value range validation.
Enter an exact decimal integer. Negative values require SLEB128 mode.
Enter one LEB128 integer. Spaces, commas, colons, hyphens and 0x prefixes are accepted.
Important: LEB128 stores seven payload bits per byte, least-significant group first. Bit 7 is the continuation flag. ULEB128 is unsigned. SLEB128 uses sign-aware termination and sign extension; it is not the same algorithm as Protobuf ZigZag encoding.
LEB128 Result Encoded
LEB128 Hex Bytes
Decimal Value
LEB128 Type
Hex Bytes
Byte Count
Encoded Bits
Integer Hex
Canonical Encoding
Range Status
Byte Hex Binary Continuation 7 Payload Bits Shift Contribution
LEB128 Calculation Breakdown -
LEB128 Hex Output
-

What Is LEB128?

LEB128 stands for Little Endian Base 128. It is a variable-length integer representation that stores seven useful data bits in each byte and uses the most significant bit as a continuation flag.

The word “little endian” in LEB128 refers to the order of the seven-bit groups: the least significant group is emitted first.

LEB128 appears in formats and virtual-machine ecosystems where compact integer representation is useful, including WebAssembly and DWARF-related data.

ULEB128 vs SLEB128

ULEB128

Unsigned LEB128 represents integers from zero upward. No sign extension is required during decoding.

SLEB128

Signed LEB128 represents positive and negative integers. The sign bit of the final seven-bit group affects termination and decoding.

How ULEB128 Encoding Works

ULEB128 repeatedly takes the lowest seven bits of the integer:

byte = value & 0x7F value = value >> 7

If another group remains, bit 7 of the emitted byte is set:

byte = byte | 0x80

The process stops once no higher payload bits remain.

ULEB128 Example: 624485

A classic LEB128 example is decimal 624485.

The encoded result is:

624485 decimal → E5 8E 26

The payload contributions are:

E5 & 7F = 65 8E & 7F = 14 26 & 7F = 38 65 + (14 << 7) + (38 << 14) = 624485

Why E5 8E 26 Decodes to 624485

The first two bytes have their continuation bit set:

E5 = 11100101 8E = 10001110 26 = 00100110

Removing bit 7 leaves seven-bit payload groups:

E5 → 1100101 8E → 0001110 26 → 0100110

These groups are applied at bit positions 0, 7 and 14 respectively.

ULEB128 127 vs 128

Values through 127 fit entirely in seven bits:

127 → 7F

Decimal 128 needs another seven-bit group:

128 → 80 01

The first byte carries seven zero payload bits and a continuation flag, while the second byte contributes one at bit position 7.

How SLEB128 Works

Signed LEB128 also extracts seven bits at a time, but the stopping rule must preserve the sign of the original integer.

Encoding can finish when the remaining shifted value is zero and the final payload’s sign bit is clear, or when the remaining value is negative one and the final payload’s sign bit is set.

This sign-aware stopping rule is what distinguishes SLEB128 from simply running ULEB128 on a negative integer.

SLEB128 Example: −1

Signed negative one has a compact one-byte representation:

-1 → 7F

The low seven payload bits are all ones and the sign bit of the final payload is set, allowing the decoder to sign-extend the value back to −1.

SLEB128 Example: −624485

Decimal −624485 is commonly represented as:

-624485 → 9B F1 59

When the final byte is reached, its sign bit indicates that the accumulated integer must be sign-extended.

LEB128 Continuation Bit

Bit 7 of every encoded byte tells the decoder whether another LEB128 byte belongs to the same integer.

bit 7 = 1 → another byte follows bit 7 = 0 → final byte

A sequence that ends while the final continuation bit remains one is truncated.

Seven Payload Bits per Byte

Although every LEB128 byte occupies eight physical bits, only seven bits carry the integer payload.

Byte layout: C D D D D D D D C = continuation bit D = payload bit

Each additional byte therefore increases the represented payload by another seven bit positions.

LEB128 Byte Ranges

ULEB128 Length Typical Unsigned Range
1 byte 0 – 127
2 bytes 128 – 16,383
3 bytes 16,384 – 2,097,151
4 bytes 2,097,152 – 268,435,455
5 bytes Up through at least the complete 32-bit unsigned range
Up to 10 bytes Complete 64-bit unsigned range

Canonical LEB128 Encoding

The same numerical value can sometimes be represented by an unnecessarily long sequence if redundant extension groups are accepted.

A canonical or minimal LEB128 representation uses the shortest encoding produced by the normal encoder.

ULEB128 zero: Canonical: 00 Overlong form: 80 00

The decoder re-encodes the value and compares the bytes so you can identify non-minimal input.

LEB128 and Little Endian

LEB128 should not be confused with simply writing an ordinary fixed-width little-endian integer. It uses variable-length seven-bit groups, not eight-bit fixed-width chunks.

Fixed Little Endian

Integer bytes use normal eight-bit groups in least-significant-byte-first order.

LEB128

Integer payload is divided into seven-bit groups with a continuation flag added to each byte.

LEB128 vs Protobuf Varint

Unsigned protobuf varints and ULEB128 use essentially the same base-128 least-significant-group-first representation for non-negative integers.

The signed behavior differs depending on the surrounding format. Protobuf’s sint32 and sint64 fields apply ZigZag before an unsigned varint, while SLEB128 directly uses sign-aware base-128 encoding.

Do not use Protobuf ZigZag rules when a format specifically requires SLEB128.

LEB128 vs ZigZag

SLEB128

Encodes the signed integer directly with sign-aware termination and sign extension.

ZigZag + Varint

First maps signed values to unsigned values, then applies an unsigned base-128 varint.

These methods can produce different byte sequences for the same negative integer.

32-Bit and 64-Bit LEB128

The LEB128 algorithm itself is variable length, but file formats and virtual machines often constrain the accepted numeric width.

This calculator provides explicit 32-bit and 64-bit validation so an encoding that mathematically represents an integer is not automatically claimed to fit a smaller target integer type.

Why This Calculator Uses BigInt

JavaScript’s normal Number representation cannot exactly store every 64-bit integer. Values above its exact-integer range may silently lose low-order bits.

All integer conversion in this LEB128 calculator uses BigInt so values such as the maximum uint64 remain exact.

ULEB128 64-Bit Maximum

The maximum unsigned 64-bit value is:

18,446,744,073,709,551,615 = 0xFFFFFFFFFFFFFFFF

Its ULEB128 representation requires ten bytes:

FF FF FF FF FF FF FF FF FF 01

LEB128 Applications

WebAssembly

WebAssembly binary encoding uses LEB128-style variable-length integer fields.

DWARF Data

Debugging information commonly uses unsigned and signed LEB128 values.

Binary File Analysis

LEB128 appears in compact binary structures where integer size varies widely.

Compiler Development

Encoders and decoders are useful when validating generated binary metadata and instruction streams.

Common LEB128 Decoding Errors

Reading Seven-Bit Groups Backward

The first payload group contributes the least significant bits.

Including the Continuation Bit

Bit 7 indicates continuation and is not one of the seven payload bits.

Using ULEB for Negative Values

Negative integers require SLEB128 or another signed encoding defined by the format.

Forgetting SLEB Sign Extension

The final payload’s sign bit determines whether the decoded result must be extended negatively.

Accepting Truncated Bytes

A sequence ending with continuation bit one is incomplete.

Ignoring Target Width

A decoded mathematical integer can still exceed a format’s 32-bit or 64-bit permitted range.

LEB128 Encoder & Decoder FAQs

What does LEB128 stand for?
LEB128 stands for Little Endian Base 128.
What is ULEB128?
ULEB128 is the unsigned form of LEB128 and represents non-negative integers using seven payload bits per encoded byte.
What is SLEB128?
SLEB128 is the signed form of LEB128 and uses sign-aware termination and sign extension to represent positive and negative integers.
How is 624485 encoded in ULEB128?
Decimal 624485 is encoded as E5 8E 26.
What does E5 8E 26 decode to?
As ULEB128 it decodes to decimal 624485.
How is 128 encoded in ULEB128?
Decimal 128 is encoded as 80 01.
How is 127 encoded in ULEB128?
Decimal 127 fits in one seven-bit payload group and is encoded as 7F.
How is −1 encoded in SLEB128?
Signed negative one is represented by the single byte 7F.
How is −624485 encoded in SLEB128?
The signed LEB128 representation is 9B F1 59.
What is the LEB128 continuation bit?
Bit 7 of each byte. One means another LEB128 byte follows and zero means the current byte is the final byte of the integer.
How many payload bits are in each LEB128 byte?
Seven bits carry integer data. The eighth bit is used as the continuation flag.
Is ULEB128 the same as a protobuf unsigned varint?
For ordinary non-negative integer encoding, they use the same basic least-significant-seven-bit-group-first base-128 representation.
Is SLEB128 the same as protobuf ZigZag?
No. SLEB128 directly represents signed values, while protobuf sint fields ZigZag-map the signed value to an unsigned value before varint encoding.
What is a canonical LEB128 value?
It is the minimal byte representation produced without unnecessary extension groups.
Can LEB128 represent 64-bit integers?
Yes. The complete unsigned 64-bit range can be represented in at most ten ULEB128 bytes.
Why does LEB128 use little-endian in its name?
The least significant seven-bit payload group is emitted first.
What happens if the final LEB128 byte has bit 7 set?
The sequence is incomplete because that continuation bit says another byte must follow.
Can an LEB128 encoding be longer than necessary?
Some decoders may encounter overlong forms. This calculator identifies whether the supplied sequence matches the minimal encoding generated for that value.
Why does this calculator use BigInt?
BigInt allows exact 64-bit integer arithmetic without the precision loss that can occur with JavaScript Number values.

Encode and Decode ULEB128 and SLEB128

Enter a decimal integer or hexadecimal LEB128 byte sequence to inspect continuation bits, seven-bit payload groups, shift positions, canonical encoding and exact signed or unsigned 32-bit and 64-bit values.

Scroll to Top