Standard library · Encoding and data

encoding

Turning text and data into Base64, hexadecimal and URL encoding.

import encoding
On this page
  1. Functions

This package holds the common ways of writing bytes as plain text: Base64 for fitting binary data into JSON or email, hexadecimal for showing bytes, and URL encoding for putting text or special characters into a web address.

The larger formats, such as JSON, YAML and XML, have sub-packages of their own, listed in this same group.

Functions

Base64Encode

func Base64Encode(data: str): str
Turns text into standard Base64, padded with =.
encoding-Base64.salam
import encoding
import mem

func main:
    e := encoding.Base64Encode("hello world")
    println e
    println encoding.Base64Decode(e)
    mut ok := false
    println "[" + encoding.Base64DecodeStrict("***", ok) + "]", ok
    println encoding.Base64DecodeStrict("YWJj", ok), ok
    b := mem.Allocate(3 as u64)
    mem.Set(b, 255, 3 as u64)
    println encoding.Base64EncodeBytes(b, 3)
    out := mem.Allocate(8 as u64)
    println encoding.Base64DecodeBytes("AAEC", out, 8, ok), ok
    mem.Free(b)
    mem.Free(out)
end
OutputaGVsbG8gd29ybGQ= hello world [] false abc true //// 3 true

Base64EncodeBytes

func Base64EncodeBytes(data: void*, n: int): str
Turns n bytes of memory into Base64.

The example for this item is shown with the ones above: encoding-Base64.salam

Base64Decode

func Base64Decode(s: str): str
Turns Base64 back into text; spaces and newlines inside it are ignored.

The example for this item is shown with the ones above: encoding-Base64.salam

HexEncode

func HexEncode(data: str): str
Writes each byte of the text as two lower case hexadecimal digits.
encoding-Hex.salam
import encoding

func main:
    h := encoding.HexEncode("Hi!")
    println h, encoding.HexDecode(h)
    println encoding.HexVal("f"), encoding.HexVal("A"), encoding.HexVal("z")
    u := encoding.URLEncode("name=Ali & age=30")
    println u
    println encoding.URLDecode(u)
end
Output486921 Hi! 15 10 -1 name%3DAli%20%26%20age%3D30 name=Ali & age=30

HexVal

func HexVal(c: str): int
The value of one hexadecimal digit, or -1 for an invalid character.

The example for this item is shown with the ones above: encoding-Hex.salam

HexDecode

func HexDecode(s: str): str
Turns a hexadecimal string back into the original bytes.

The example for this item is shown with the ones above: encoding-Hex.salam

URLEncode

func URLEncode(s: str): str
Percent-encodes text for a web address; letters, digits and -_.~ are left as they are.

The example for this item is shown with the ones above: encoding-Hex.salam

URLDecode

func URLDecode(s: str): str
Undoes percent encoding and turns + back into a space.

The example for this item is shown with the ones above: encoding-Hex.salam

Base64DecodeStrict

func Base64DecodeStrict(s: str, ok &: bool): str
Like Base64Decode, but any invalid character makes ok false and returns an empty string.

The example for this item is shown with the ones above: encoding-Base64.salam

Base64DecodeBytes

func Base64DecodeBytes(s: str, out: void*, cap: int, ok &: bool): int
Decodes Base64 into the buffer out, of capacity cap, and returns how many bytes it wrote.

The example for this item is shown with the ones above: encoding-Base64.salam