Reference · reference

encoding module

1 min read · 2026-10-19 · config-weave 0.1.0

use encoding converts between strings, bytes and base64. Bytes are a List[int], the same representation fs::read_bytes returns and fs::write_bytes takes, so the two modules compose. A function that takes bytes returns Err when any element lies outside 0..=255.

base64_encode and base64_decode

rust
encoding::base64_encode(bytes: List[int]) -> Result[string, string]
encoding::base64_decode(s: string) -> Result[List[int], string]

Both use the standard base64 alphabet with = padding. base64_decode ignores leading and trailing whitespace and returns Err for any other character outside the alphabet.

utf8 and from_utf8

rust
encoding::utf8(s: string) -> List[int]
encoding::from_utf8(bytes: List[int]) -> Result[string, string]

utf8 returns a string's UTF-8 bytes. from_utf8 decodes bytes into a string and returns Err when they are not valid UTF-8. To base64 a string, encode it to bytes first.

utf16le and from_utf16le

rust
encoding::utf16le(s: string) -> List[int]
encoding::from_utf16le(bytes: List[int]) -> Result[string, string]

utf16le returns a string's UTF-16 little-endian bytes, with no byte order mark. Windows APIs and file formats that store wide strings use this form.

from_utf16le decodes UTF-16 little-endian bytes into a string. It strips a leading byte order mark (FF FE) if one is present. It returns Err when the byte count is odd or the bytes contain an unpaired surrogate; it does not replace bad input with U+FFFD.

A Windows unattend file hides a password as base64(UTF-16LE(password + suffix)), where the suffix is the element name, such as Password or AdministratorPassword:

rust
use encoding

let hidden = encoding::base64_encode(encoding::utf16le(password + "Password"))?
// write it with <PlainText>false</PlainText>