function encodeWord
encodeWord(content: string | Uint8Array): EncodedWord

Encodes a word (string or bytes) into MikroTik API format

Converts the input to UTF-8 bytes, prepends a variable-length size prefix, and returns the complete encoded word ready for transmission.

Examples

Encode a simple string

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

const encoded = encodeWord("hello");
// First byte is length (5), followed by "hello" in UTF-8
assertEquals(encoded, new Uint8Array([0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F]));

Encode a command word

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

const encoded = encodeWord("/interface/print");
assertEquals(encoded[0], 16); // Length byte
assertEquals(encoded.length, 17); // 1 byte length + 16 bytes content

Encode raw bytes

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

const bytes = new Uint8Array([0x41, 0x42, 0x43]);
const encoded = encodeWord(bytes);
assertEquals(encoded, new Uint8Array([0x03, 0x41, 0x42, 0x43]));

Parameters

content: string | Uint8Array

The string or byte array to encode

Return Type

Encoded word with length prefix and content bytes

Usage

import { encodeWord } from "encoding/word.ts";