Skip to content

Base encode/decode

decode16(binary, opts \\ [])

Decodes a base 16 encoded string into a binary string.

Options:

The accepted options are:

  • :case - specifies the character case to accept when decoding

The values for :case can be:

  • :upper - only allows upper case characters (default)
  • :lower - only allows lower case characters
  • :mixed - allows mixed case characters

Examples:

    iex> Base.decode16("666F6F626172")
    {:ok, "foobar"}

    iex> Base.decode16("666f6f626172", case: :lower)
    {:ok, "foobar"}

    iex> Base.decode16("666f6F626172", case: :mixed)
    {:ok, "foobar"}

decode32(binary, opts \\ [])

Decodes a base 32 encoded string into a binary string.

Options:

The accepted options are:

  • :case - specifies the character case to accept when decoding
  • :padding - specifies whether to require padding

The values for :case can be:

  • :upper - only allows upper case characters (default)
  • :lower - only allows lower case characters
  • :mixed - allows mixed case characters

The values for :padding can be:

  • true - requires the input string to be padded to the nearest multiple of 8 (default)
  • false - ignores padding from the input string

Examples:

iex> Base.decode32("MZXW6YTBOI======")
{:ok, "foobar"}

iex> Base.decode32("mzxw6ytboi======", case: :lower)
{:ok, "foobar"}

iex> Base.decode32("mzXW6ytBOi======", case: :mixed)
{:ok, "foobar"}

iex> Base.decode32("MZXW6YTBOI", padding: false)
{:ok, "foobar"}

decode64(binary, opts \\ [])

Decodes a base 64 encoded string into a binary string.

Accepts ignore: :whitespace option which will ignore all the whitespace characters in the input string.

Accepts padding: false option which will ignore padding from the input string.

Examples:

iex> Base.decode64("Zm9vYmFy")
{:ok, "foobar"}

iex> Base.decode64("Zm9vYmFy\n", ignore: :whitespace)
{:ok, "foobar"}

iex> Base.decode64("Zm9vYg==")
{:ok, "foob"}

iex> Base.decode64("Zm9vYg", padding: false)
{:ok, "foob"}

encode16(binary, opts \\ [])

Encodes a binary string into a base 16 encoded string.

Options:

The accepted options are:

  • :case - specifies the character case to use when encoding

The values for :case can be:

  • :upper - uses upper case characters (default)
  • :lower - uses lower case characters

Examples:

iex> Base.encode16("foobar")
"666F6F626172"

iex> Base.encode16("foobar", case: :lower)
"666f6f626172"

encode32(binary, opts \\ [])

Encodes a binary string into a base 32 encoded string.

Options:

The accepted options are:

  • :case - specifies the character case to use when encoding
  • :padding - specifies whether to apply padding

The values for :case can be:

  • :upper - uses upper case characters (default)
  • :lower - uses lower case characters

The values for :padding can be:

  • true - pad the output string to the nearest multiple of 8 (default)
  • false - omit padding from the output string

Examples:

iex> Base.encode32("foobar")
"MZXW6YTBOI======"

iex> Base.encode32("foobar", case: :lower)
"mzxw6ytboi======"

iex> Base.encode32("foobar", padding: false)
"MZXW6YTBOI"

encode64(binary, opts \\ [])

Encodes a binary string into a base 64 encoded string.

Accepts padding: false option which will omit padding from the output string.

Examples

iex> Base.encode64("foobar")
"Zm9vYmFy"

iex> Base.encode64("foob")
"Zm9vYg=="

iex> Base.encode64("foob", padding: false)
"Zm9vYg"