DevToys Web Pro iconDevToys Web ProBlog
Rate us:
Try browser extension:

VarInt Encoder / Decoder

12 bytes
Mode

Decimal values

  • VarInt output

  • Technical details

    How the VarInt Encoder / Decoder Works

    What the Tool Does

    The VarInt tool encodes unsigned integers as Protobuf-style LEB128 variable-length integers and decodes binary VarInt streams back to integer lists. Each value is encoded in 1–10 bytes depending on its magnitude, with the high bit of every byte signaling whether more bytes follow. The output is shown as a byte array, hexadecimal, or Base64 for easy embedding in tests and fixtures.

    Common Developer Use Cases

    Engineers debugging Protobuf wire-format payloads use VarInt encode/decode to inspect field tags and unsigned integer field values without spinning up a full Protobuf runtime. Bitcoin and other cryptocurrency developers parse VarInt-encoded script lengths and transaction counts. The tool is also helpful when reverse-engineering custom binary protocols that adopt LEB128 because of its compactness for small numbers.

    Data Formats, Types, or Variants

    LEB128 (Little-Endian Base 128) emits 7 payload bits per byte with the most significant bit acting as a continuation flag. Small numbers (0–127) use a single byte; numbers up to 16,383 use two bytes; and so on. Inputs accept decimal numbers separated by whitespace, commas, or newlines. Outputs are available in hexadecimal, Base64, and raw byte array notation. Signed integers can be encoded by first applying ZigZag transformation (n << 1) ^ (n >> 31).

    Common Pitfalls and Edge Cases

    JavaScript number precision tops out at 2^53 − 1; values above Number.MAX_SAFE_INTEGER cannot be encoded safely. Truncated VarInt streams (missing terminator byte) decode partially and return an error indicating the offset. Negative numbers cannot be encoded directly — use ZigZag encoding first. Avoid copying VarInt bytes from screenshots where Base64 padding may have been stripped, as missing padding alters byte boundaries.

    When to Use This Tool vs Code

    Use the browser tool for quick inspection of Protobuf wire bytes during debugging or while writing tests. For production encoding, use language-native VarInt libraries (`varint` on npm, `binary.varint` in Go's encoding/binary, `decode_varint` in Python's google.protobuf.internal) which handle streaming, error recovery, and ZigZag transformation in a single call.