Utilities

Low-level helpers for encoding/decoding binary data and text.

All functions treat string as a raw byte container, so they work fine with binary data (including \0 bytes and non-UTF8).

Functions

string util_base64_encode(const string &in data)
bool   util_base64_decode(const string &in text, string &out data, string &out error)

string util_hex_encode(const string &in data)
bool   util_hex_decode(const string &in text, string &out data, string &out error)

string util_url_encode(const string &in data)
string util_url_decode(const string &in data)

Base64 helpers

string util_base64_encode(const string &in data)

Encodes arbitrary data as Base64 text.

  • Parameters

    • data — Input bytes to encode (any string, including binary).

  • Returns

    • Base64-encoded text using the standard alphabet A–Z a–z 0–9 + / with = padding.

Example


bool util_base64_decode(const string &in text, string &out data, string &out error)

Decodes a Base64 string back into raw bytes.

  • Parameters

    • text — Base64 text to decode.

    • data (out) — Output decoded bytes on success.

    • error (out) — Error message when returning false.

  • Returns

    • true on success, false on error.

On success

  • data contains decoded bytes.

  • error is empty.

On failure

  • data is empty.

  • error contains a human-readable message (e.g. "Invalid base64 character").

Example

Error example


Hex helpers

string util_hex_encode(const string &in data)

Encodes binary data as uppercase hexadecimal text.

  • Parameters

    • data — Raw bytes to encode.

  • Returns

    • Hex string using 0–9 A–F. Each byte becomes 2 characters.

Example


bool util_hex_decode(const string &in text, string &out data, string &out error)

Decodes a hex string back into raw bytes.

  • Parameters

Name
Type
Description

text

string

Hex text (must have even length)

data

string (out)

Output decoded bytes on success

error

string (out)

Error message when returning false

  • Returns

    • true on success, false on error.

On success

  • data contains decoded bytes.

  • error is empty.

On failure

  • data is empty.

  • error contains a message.

Example

Error examples


URL helpers

These functions use standard URL percent-encoding:

  • Unreserved characters stay as-is: A–Z a–z 0–9 - _ . ~

  • Everything else becomes %XX (hex byte).

Note: util_url_decode does not convert + to space. + is treated as a normal character.

string util_url_encode(const string &in data)

Percent-encodes a string for safe use in URLs (query params, etc).

  • Parameters

    • data — Input string (can be binary, but typically text).

  • Returns

    • URL-encoded string.

Example


string util_url_decode(const string &in data)

Decodes %XX sequences back into raw bytes.

  • Parameters

    • data — URL-encoded string (e.g. "hello%20world").

  • Returns

    • Decoded string with %XX sequences converted to bytes.

    • Invalid % sequences are left as literal characters.

Example


Roundtrip examples

Base64 roundtrip

Hex roundtrip

URL roundtrip


Binary safety

All util_* functions operate on string as byte containers:

  • You can safely encode/decode binary data.

  • Data with \0 bytes is preserved.

  • Non-UTF8 content is preserved.

Useful for:

  • Network packets

  • Encryption keys

  • Hash outputs

  • Serialized structs / blobs


hash_set

hash_set is a lightweight unordered_set<uint64> wrapper for fast membership tests (O(1) average).

Type

hash_set

Methods

Core

  • bool hash_set::contains(uint64 v) const Returns true if v exists in the set.

  • bool hash_set::insert(uint64 v) Inserts v. Returns true if it was newly inserted, false if it already existed.

  • bool hash_set::erase(uint64 v) Removes v. Returns true if an element was removed, false if it wasn’t present.

  • void hash_set::clear() Removes all elements.

  • uint hash_set::size() const Returns the number of elements.

  • bool hash_set::empty() const Returns true if the set is empty.


Convenience

  • void hash_set::set(uint64 v) Inserts v without returning a status.

  • bool hash_set::get(uint64 v, uint64 &out value) Checks if v exists and outputs the stored value.


Iteration

  • void hash_set::iter_begin() Resets the internal iterator to the start.

  • bool hash_set::iter_next(uint64 &out value) Retrieves the next value in the set. Returns false when iteration is finished.


hash_map

hash_map is a lightweight unordered map that associates uint64 keys with any AngelScript type (primitives, strings, objects, and handles).

All operations run in O(1) average time.

Type

hash_map

Methods

Construction

  • hash_map@ hash_map() Factory constructor.


Core

  • void hash_map::set(uint64 key, ?&in value) Stores a value for the given key.

  • bool hash_map::get(uint64 key, ?&out value) Retrieves the value associated with key. Returns true if found.

  • bool hash_map::contains(uint64 key) Returns true if the key exists.

  • bool hash_map::erase(uint64 key) Removes the entry for key. Returns true if removed.

  • void hash_map::clear() Removes all entries.

  • uint hash_map::size() Returns the number of stored entries.

  • bool hash_map::empty() Returns true if the map is empty.


Iteration

  • void hash_map::iter_begin() Resets the internal iterator to the start.

  • bool hash_map::iter_next_key(uint64 &out key) Iterates over keys only.

  • bool hash_map::iter_next(uint64 &out key, ?&out value) Iterates over key-value pairs.

Example (hash_set & hash_map)

Last updated