function encodeLength
encodeLength(length: number): EncodedLength

Encodes a length value into MikroTik API format

Uses variable-length encoding where the first byte(s) indicate the size:

  • 1 byte for 0-127
  • 2 bytes for 128-16,383
  • 3 bytes for 16,384-2,097,151
  • 4 bytes for 2,097,152-268,435,455
  • 5 bytes for 268,435,456-34,359,738,367

Examples

Encode small lengths (1 byte)

import { assertEquals } from "@std/assert";
import { encodeLength } from "@hertzg/routeros-api/encoding/length";

const encoded = encodeLength(42);
assertEquals(encoded, new Uint8Array([0x2A]));
assertEquals(encoded.length, 1);

Encode medium lengths (2 bytes)

import { assertEquals } from "@std/assert";
import { encodeLength } from "@hertzg/routeros-api/encoding/length";

const encoded = encodeLength(1000);
assertEquals(encoded, new Uint8Array([0x83, 0xE8]));
assertEquals(encoded.length, 2);

Encode large lengths (5 bytes)

import { assertEquals } from "@std/assert";
import { encodeLength } from "@hertzg/routeros-api/encoding/length";

const encoded = encodeLength(268435456);
assertEquals(encoded.length, 5);
assertEquals(encoded[0], 240); // First byte indicates 5-byte encoding

Parameters

length: number

The non-negative integer length to encode (max 0x7FFFFFFFF)

Return Type

Encoded length as a Uint8Array (1-5 bytes)