BSV.Util (BSV v2.1.0) View Source

Collection of shared helper functions, used frequently throughout the library.

Link to this section Summary

Types

Binary encoding format

Functions

Decodes the given binary data using the specified BSV.Util.encoding/0.

Decodes the given binary data using the specified BSV.Util.encoding/0.

Encodes the given binary data using the specified BSV.Util.encoding/0.

Returns a binary containing the specified number of random bytes.

Reverses the bytes of the given binary data.

Link to this section Types

Specs

encoding() :: :base64 | :hex

Binary encoding format

Link to this section Functions

Specs

decode(binary(), encoding()) :: {:ok, binary()} | {:error, term()}

Decodes the given binary data using the specified BSV.Util.encoding/0.

Returns the result in an :ok / :error tuple pair.

Examples

iex> BSV.Util.decode("aGVsbG8gd29ybGQ=", :base64)
{:ok, "hello world"}

iex> BSV.Util.decode("68656c6c6f20776f726c64", :hex)
{:ok, "hello world"}

Specs

decode!(binary(), encoding()) :: binary()

Decodes the given binary data using the specified BSV.Util.encoding/0.

As decode/2 but returns the result or raises an exception.

Specs

encode(binary(), encoding()) :: binary()

Encodes the given binary data using the specified BSV.Util.encoding/0.

Examples

iex> BSV.Util.encode("hello world", :base64)
"aGVsbG8gd29ybGQ="

iex> BSV.Util.encode("hello world", :hex)
"68656c6c6f20776f726c64"

Specs

rand_bytes(integer()) :: binary()

Returns a binary containing the specified number of random bytes.

Specs

reverse_bin(binary()) :: binary()

Reverses the bytes of the given binary data.

Examples

iex> BSV.Util.reverse_bin("abcdefg")
"gfedcba"

iex> BSV.Util.reverse_bin(<<1, 2, 3, 0>>)
<<0, 3, 2, 1>>