CrockfordBase32 (crockford_base32 v0.1.0)
The main module implements Douglas Crockford's Base32 encoding.
Link to this section Summary
Functions
Decode the encoded to an string, all hyphen(s) are removed and ignore the encoded's case.
Decode the encoded to an integer, all hyphen(s) are removed and ignore the encoded's case.
Encode an integer or a binary in Crockford's Base32.
Link to this section Functions
decode_to_binary(string, opts \\ [])
Decode the encoded to an string, all hyphen(s) are removed and ignore the encoded's case.
If the encoded string be with a check symbol, require to use checksum: true
in decoding.
example
Example
iex> CrockfordBase32.decode_to_binary("C5H66")
{:ok, "abc"}
iex> CrockfordBase32.decode_to_binary("C5H66C", checksum: true)
{:ok, "abc"}
iex> CrockfordBase32.decode_to_binary("C5H66D", checksum: true)
{:error, "invalid_checksum"}
options
Options
The same to the options of decode_to_integer/2
.
decode_to_integer(string, opts \\ [])
Specs
Decode the encoded to an integer, all hyphen(s) are removed and ignore the encoded's case.
If the encoded string be with a check symbol, require to use checksum: true
in decoding.
example
Example
iex> CrockfordBase32.decode_to_integer("16JD", checksum: true)
{:ok, 1234}
iex> CrockfordBase32.decode_to_integer("16j")
{:ok, 1234}
iex> CrockfordBase32.decode_to_integer("16j*", checksum: true)
{:error, "invalid_checksum"}
options
Options
:checksum
, optional, a boolean, by defaults tofalse
means expect input the encoded string without a check symbol in its tail, if set it astrue
, please ensure input encoded is a string be with a check symbol, or return{:error, "invalid_checksum"}
.
encode(value, opts \\ [])
Specs
Encode an integer or a binary in Crockford's Base32.
After encoded, the return string only contains these characters set("0123456789ABCDEFGHJKMNPQRSTVWXYZ"
, 10 digits and
22 letters, excluding "I"
, "L"
, "O"
and "U"
), if set checksum: true
, there would be with one of these check
symbols("*~$=U"
) or the previous 10 digits and 22 letters as the last character.
example
Example
iex> CrockfordBase32.encode(1234567)
"15NM7"
iex> CrockfordBase32.encode(1234567, checksum: true)
"15NM7S"
iex> CrockfordBase32.encode(1234567, split_size: 3)
"15N-M7"
iex> CrockfordBase32.encode(1234567, split_size: 3, checksum: true)
"15N-M7S"
options
Options
:checksum
, optional, a boolean, by defaults tofalse
, if set it astrue
will calculate a check symbol and append it to the return string.:split_size
, optional, a positive integer, if set it will use it as a step size to split the return string with hyphen(s).