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.
| Byte | Hex | Binary | Continuation | 7 Payload Bits | Shift | Contribution |
|---|
-
-
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.
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.
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?
What is ULEB128?
What is SLEB128?
How is 624485 encoded in ULEB128?
What does E5 8E 26 decode to?
How is 128 encoded in ULEB128?
How is 127 encoded in ULEB128?
How is −1 encoded in SLEB128?
How is −624485 encoded in SLEB128?
What is the LEB128 continuation bit?
How many payload bits are in each LEB128 byte?
Is ULEB128 the same as a protobuf unsigned varint?
Is SLEB128 the same as protobuf ZigZag?
What is a canonical LEB128 value?
Can LEB128 represent 64-bit integers?
Why does LEB128 use little-endian in its name?
What happens if the final LEB128 byte has bit 7 set?
Can an LEB128 encoding be longer than necessary?
Why does this calculator use BigInt?
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.