Rat docs

Encoding

Three pairs of complementary builtins for the encodings every web app eventually needs. base64 for embedding binary payloads in JSON or data URLs; url for safely building query strings; hex for low-level byte dumps and short identifiers. Each pair round-trips — decode(encode(x)) == x.

base64_encode / base64_decode

Standard alphabet; padding included

base64_encode(s) turns any string into the standard Base64 alphabet. base64_decode(s) reverses it. Use it for embedding files in JSON, building data: URLs, or carrying binary payloads across the dialect boundary into Python or JS.

<p> [base64_encode('hi rat')]
<p> [base64_decode('aGkgcmF0')]
Result

encoded: aGkgcmF0

decoded: hi rat

url_encode / url_decode

Query-string escaping; space → +

url_encode(s) escapes a string for use in a query parameter or path segment. Reserved characters become percent-escapes; space becomes + (the application/x-www-form-urlencoded rule). url_decode reverses it.

<p> [url_encode('hello world & rat')]
<p> [url_decode('hello+world+%26+rat')]
Result

encoded: hello+world+%26+rat

decoded: hello world & rat

hex_encode / hex_decode

Lowercase pairs, no separator

hex_encode(s) renders a string as lowercase hex pairs (two characters per byte, no separator, no 0x prefix). hex_decode(s) reverses it. Common uses: short stable identifiers from a hash, compact representation of small binary buffers in logs.

<p> [hex_encode('hi')]
<p> [hex_decode('6869')]
Result

encoded: 6869

decoded: hi

Composing with http

Build a query string safely

The encoding builtins are the glue between user-provided strings and URL construction. Encode every dynamic segment — never assume the input is already URL-safe.

> server
q: 'cats & dogs'
url: 'https://example.com/search?q=' + url_encode(q)
resp: http.get(url)

Next: HTTP — outbound requests with the same shape as services and DBs.