decodeWord(bytes: Uint8Array,options?: { offset?: number; }): DecodedWord
Decodes a word from MikroTik API format
Reads the variable-length size prefix, extracts the content bytes, and decodes them as a UTF-8 string.
Decode a simple word
Decode a simple word
import { assertEquals } from "@std/assert"; import { decodeWord } from "@hertzg/routeros-api/encoding/word"; const buffer = new Uint8Array([0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F]); const result = decodeWord(buffer); assertEquals(result.word, "hello"); assertEquals(result.bytesRead, 6);
Decode with offset
Decode with offset
import { assertEquals } from "@std/assert"; import { decodeWord } from "@hertzg/routeros-api/encoding/word"; const buffer = new Uint8Array([0xFF, 0xFF, 0x03, 0x41, 0x42, 0x43]); const result = decodeWord(buffer, { offset: 2 }); assertEquals(result.word, "ABC"); assertEquals(result.bytesRead, 4);
Round-trip encoding and decoding
Round-trip encoding and decoding
import { assertEquals } from "@std/assert"; import { encodeWord, decodeWord } from "@hertzg/routeros-api/encoding/word"; const original = "/interface/print"; const encoded = encodeWord(original); const decoded = decodeWord(encoded); assertEquals(decoded.word, original); assertEquals(decoded.bytesRead, encoded.length);
bytes: Uint8Array
The byte array containing the encoded word
Object containing the decoded string and total bytes consumed