decodeSentence(bytes: Uint8Array,options?: { offset?: number; }): DecodedSentence
Decodes a sentence from MikroTik API format
Reads words sequentially until encountering a zero byte (0x00) terminator. Each word is decoded with its length prefix.
Decode a simple command
Decode a simple command
import { assertEquals } from "@std/assert"; import { decodeSentence } from "@hertzg/routeros-api/encoding/sentence"; // Sentence: "/login" (length 6) + terminator const buffer = new Uint8Array([0x06, 0x2F, 0x6C, 0x6F, 0x67, 0x69, 0x6E, 0x00]); const result = decodeSentence(buffer); assertEquals(result.words, ["/login"]); assertEquals(result.bytesRead, 8);
Decode with offset
Decode with offset
import { assertEquals } from "@std/assert"; import { decodeSentence } from "@hertzg/routeros-api/encoding/sentence"; const buffer = new Uint8Array([0xFF, 0xFF, 0x03, 0x66, 0x6F, 0x6F, 0x00]); const result = decodeSentence(buffer, { offset: 2 }); assertEquals(result.words, ["foo"]); assertEquals(result.bytesRead, 5);
Round-trip encoding and decoding
Round-trip encoding and decoding
import { assertEquals } from "@std/assert"; import { encodeSentence, decodeSentence } from "@hertzg/routeros-api/encoding/sentence"; const original = ["/interface/print", "=.proplist=name"]; const encoded = encodeSentence(original); const decoded = decodeSentence(encoded); assertEquals(decoded.words, original); assertEquals(decoded.bytesRead, encoded.length);
bytes: Uint8Array
The byte array containing the encoded sentence
Object containing the array of words and total bytes consumed