base_x_gleam
Base-X encoding and decoding implementation in Gleam!
Allows the creation of encoders and decoders for arbitrary character sets (alphabets).
Can be used for encoding and decoding hexadecimal and base-64, but is mostly most useful for more archaic alphabets (like base58 used in some blockchain applications).
Currently only supports the Erlang target.
Also see the Elixir BaseX
library and the Javascript base-x
npm package
gleam add base_x_gleam
import base_x_gleam
import gleam/bit_array
pub fn main() {
// base58 alphabet
let b58_alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
// Will return an error if the alphabet contains duplicates
let assert Ok(b58_encoder, b58_decoder) = base_x_gleam.generate(alphabet)
"foobar"
|> bit_array.from_string
|> b58_encoder // "t1Zv2yaZ"
"3FhSYN5Ra1ZtmGfAC"
|> b58_decoder // Ok(<<"wibblewobble">>)
}
Further documentation can be found at https://hexdocs.pm/base_x_gleam.