BSV v0.1.0-dev.2 BSV.Util View Source

A collection of commonly used helper methods.

Link to this section Summary

Functions

Decodes the given binary data with the specified encoding scheme.

Encodes the given binary data with the specified encoding scheme.

Generates random bits for the given number of bytes.

Encodes the given integer into a variable length binary.

Link to this section Functions

Link to this function

decode(data, encoding)

View Source
decode(binary(), atom()) :: binary()

Decodes the given binary data with the specified encoding scheme.

Options

The accepted encoding schemes are:

  • :base64 - Encodes the binary using Base64
  • :hex - Encodes the binary using Base16 (hexidecimal), using lower character case

Examples

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

iex> BSV.Util.decode("68656c6c6f20776f726c64", :hex)
"hello world"
Link to this function

encode(data, encoding)

View Source
encode(binary(), atom()) :: binary()

Encodes the given binary data with the specified encoding scheme.

Options

The accepted encoding schemes are:

  • :base64 - Encodes the binary using Base64
  • :hex - Encodes the binary using Base16 (hexidecimal), using lower character case

Examples

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

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

random_bytes(bytes)

View Source
random_bytes(integer()) :: binary()

Generates random bits for the given number of bytes.

Examples

iex> iv = BSV.Util.random_bytes(16)
...> bit_size(iv)
128

Encodes the given integer into a variable length binary.

Examples

iex> BSV.Util.varint(250)
<<250>>

iex> BSV.Util.varint(9128)
<<253, 168, 35>>

iex> BSV.Util.varint(389128)
<<254, 8, 240, 5, 0>>

iex> BSV.Util.varint(389128)
<<254, 8, 240, 5, 0>>

iex> BSV.Util.varint(51258291273926)
<<255, 198, 64, 62, 128, 158, 46, 0, 0>>