function decodeLength
decodeLength(
bytes: Uint8Array,
options?: { offset?: number; }
): DecodedLength

Decodes a length value from MikroTik API format

Reads the variable-length encoded integer from the buffer starting at the specified offset. The first byte indicates how many bytes to read (1-5).

Examples

Decode single-byte length

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

const result = decodeLength(new Uint8Array([0x2A]));
assertEquals(result.length, 42);
assertEquals(result.bytesRead, 1);

Decode with offset

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

const buffer = new Uint8Array([0xFF, 0xFF, 0x83, 0xE8]);
const result = decodeLength(buffer, { offset: 2 });
assertEquals(result.length, 1000);
assertEquals(result.bytesRead, 2);

Round-trip encoding and decoding

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

const original = 65535;
const encoded = encodeLength(original);
const decoded = decodeLength(encoded);

assertEquals(decoded.length, original);
assertEquals(decoded.bytesRead, encoded.length);

Parameters

The byte array containing the encoded length

optional
options: { offset?: number; }

Optional decoding options

Return Type

Object containing the decoded length and bytes consumed