textEncode(text, [encoding]) Last updated: 17. Oct 2026
Converts a string into bytes using the specified encoding.
Parameters
| Name | Type | Description |
|---|---|---|
| text | string | The string to encode into bytes. |
| encoding (optional) | string | utf8 (default, also utf-8), utf32, unicode (UTF-16 little-endian, also utf), bigendianunicode, ascii, utf7 or default (UTF-8). Case-insensitive. An unknown name returns null. |
Returns
A byte array (not a Node.js Buffer). null if text is null or the encoding is unknown.
Example
Code example (JS)
JS is normal JavaScript either running in the browser or on the Docly™ server.// Encoding a string into bytes
let bytes = docly.textEncode("Hello, docly!", "utf8");
// Decoding bytes back into a string
let str = docly.textDecode(bytes, "utf8");
write(str); // Outputs: Hello, docly! Output
The JS code above produces the output shown below:Hello, docly!