Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

std/encoding/varint.ts>encodeVarint

The Deno Standard Library
Latest
function encodeVarint
import { encodeVarint } from "https://deno.land/std@0.224.0/encoding/varint.ts";

Takes unsigned number num and converts it into a VarInt encoded Uint8Array, returning a tuple consisting of a Uint8Array slice of the encoded VarInt, and an offset where the VarInt encoded bytes end within the Uint8Array.

If buf is not given then a Uint8Array will be created. offset defaults to 0.

If passed buf then that will be written into, starting at offset. The resulting returned Uint8Array will be a slice of buf. The resulting returned number is effectively offset + bytesWritten.

Examples

Example 1

import { encodeVarint } from "https://deno.land/std@0.224.0/encoding/varint.ts";

const buf = new Uint8Array(10);
encodeVarint(42n, buf); // [ Uint8Array(1) [ 42 ], 1 ];

Parameters

num: bigint | number

The number to encode.

optional
buf: Uint8Array = [UNSUPPORTED]

The buffer to write into.

optional
offset = [UNSUPPORTED]

The offset to start writing at.

Returns

[Uint8Array, number]

A tuple of the encoded VarInt Uint8Array and the new offset.