encode
The encode package converts data to and from text encodings.
println encode::base64_encode("kiwi") # a2l3aQ==println encode::base64_decode("a2l3aQ==").to_string() # kiwi
println encode::hex_encode("kiwi") # 6b697769println encode::hex_decode("6b697769").to_string() # kiwi
println encode::url_encode("a b&c") # a+b%26cprintln encode::url_decode("a+b%26c") # a b&cThe encoders accept a string or bytes. base64_decode and hex_decode return bytes, since the decoded data may not be text; call .to_string() to decode it as UTF-8.
Strings also have .base64encode(), .base64decode(), .urlencode() and .urldecode() methods from the string package. For hashing, see crypto.
Functions
Section titled “Functions”encode::base64_encode
Section titled “encode::base64_encode”encode::base64_encode(data)Encodes a string or bytes to Base64.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | string|bytes|list | The data to encode. |
Returns string — Base64-encoded string.
encode::base64_decode
Section titled “encode::base64_decode”encode::base64_decode(data)Decodes a Base64 string to bytes.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | string | Base64-encoded string. |
Returns bytes — Decoded bytes.
encode::hex_encode
Section titled “encode::hex_encode”encode::hex_encode(data)Encodes a string or bytes to lowercase hex.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | string|bytes|list | The data to encode. |
Returns string — Hex-encoded string.
encode::hex_decode
Section titled “encode::hex_decode”encode::hex_decode(hex_str)Decodes a hex string to bytes.
| Parameter | Type | Default | Description |
|---|---|---|---|
hex_str | string | Hex-encoded string (spaces and dashes ignored). |
Returns bytes — Decoded bytes.
encode::url_encode
Section titled “encode::url_encode”encode::url_encode(str)URL-encodes a string.
| Parameter | Type | Default | Description |
|---|---|---|---|
str | string | String to encode. |
Returns string — URL-encoded string.
encode::url_decode
Section titled “encode::url_decode”encode::url_decode(str)URL-decodes a string.
| Parameter | Type | Default | Description |
|---|---|---|---|
str | string | URL-encoded string. |
Returns string — Decoded string.