decodeVarint32

import { decodeVarint32 } from "jsr:@ende/encoding";

function

function decodeVarint32(buf: Uint8Array, offset: number = 0): [number, number]

Given a buf, starting at offset (default: 0), begin decoding bytes as Varint encoded bytes, for a maximum of 5 bytes (offset + 5). The returned tuple is of the decoded varint 32-bit number, and the new offset with which to continue decoding other data.

Varints are not 32-bit by default so this should only be used in cases where the varint is assured to be 32-bits. If in doubt, use decode().

To know how many bytes the Varint took to encode, simply negate offset from the returned new offset.

buf: Uint8Array

The buffer to decode from.

offset: number = 0

The offset to start decoding from.

Returns

A tuple of the decoded varint 32-bit number, and the new offset.

Example: Usage

import { decodeVarint32 } from "@ende/encoding/varint";
import { assertEquals } from "@ende/assert";

const buf = new Uint8Array([0x8E, 0x02]);
assertEquals(decodeVarint32(buf), [270, 2]);

Source