encoding
Turning text and data into Base64, hexadecimal and URL encoding.
import encoding
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=.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)
endBase64EncodeBytes
func Base64EncodeBytes(data: void*, n: int): strn bytes of memory into Base64.The example for this item is shown with the ones above: encoding-Base64.salam
Base64Decode
func Base64Decode(s: str): strThe example for this item is shown with the ones above: encoding-Base64.salam
HexEncode
func HexEncode(data: str): strimport 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)
endHexVal
func HexVal(c: str): intThe example for this item is shown with the ones above: encoding-Hex.salam
HexDecode
func HexDecode(s: str): strThe example for this item is shown with the ones above: encoding-Hex.salam
URLEncode
func URLEncode(s: str): str-_.~ 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+ 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): strBase64Decode, 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): intout, of capacity cap, and returns how many bytes it wrote.The example for this item is shown with the ones above: encoding-Base64.salam