LEB128 Encoder/Decoder

Encode integers to WASM-style Little Endian Base 128 format

Ad placeholder (leaderboard)

LEB128, short for Little Endian Base 128, is the variable-length integer encoding used inside WebAssembly modules and the DWARF debugging format. This tool converts an integer to its LEB128 byte sequence and back, in both the unsigned and the signed (SLEB128) variants.

How it works

Each output byte carries 7 bits of the number in its low bits and a continuation flag in its high bit (0x80). For unsigned encoding the value is shifted right 7 bits at a time; the continuation bit is set on every byte except the last:

624485 -> E5 8E 26
  byte0 = 0xE5 (continuation set)
  byte1 = 0x8E (continuation set)
  byte2 = 0x26 (last byte)

Signed encoding (SLEB128) uses an arithmetic right shift so the sign propagates, and it stops once the remaining value is 0 (for non-negatives) or -1 (for negatives) and the sign bit of the final 7-bit group already matches. Decoding reverses the process: combine each group with a left shift of 7 times its index, and for signed values sign-extend if the final group’s bit 0x40 is set.

Tips and notes

When decoding, separate bytes with spaces or commas; tokens like E5, 0xE5, or 229 are all accepted. The last byte you supply must have its continuation bit cleared, otherwise the input is reported as incomplete. Encoding the value 0 produces a single zero byte, which is the canonical minimal form. For signed data always select SLEB128 — decoding signed bytes as unsigned will misread the sign extension.

Ad placeholder (rectangle)