BSV.VarInt (BSV v2.1.0) View Source
A VarInt is an integer encoded as a variable length binary value. It is a format used throughout Bitcoin to represent the length of binary data in a compact form.
Link to this section Summary
Functions
Decodes the given VarInt binary into an integer.
Decodes the given VarInt binary into an integer.
Returns a binary of the length specified by the VarInt in the first bytes of the binary. Any remaining bytes are ignored.
Returns a binary of the length specified by the VarInt in the first bytes of the binary.
Encodes the given integer into a VarInt binary.
Prepends the given binary with a VarInt representing the length of the binary.
Parses the given binary, returning a tuple with a binary of the length specified by the VarInt in the first bytes of the binary, and a binary of any remaining bytes.
Parses the given binary, returning a tuple with an integer decoded from the VarInt in the first bytes of the binary, and a binary of any remaining bytes.
Parses the given binary into a list of the length specified by the VarInt in
the first bytes of the binary. Each item is parsed according to the specified
Serializable.t/0
.
Link to this section Types
Specs
t() :: binary()
VarInt binary
Link to this section Functions
Specs
Decodes the given VarInt binary into an integer.
Returns the result in an :ok
/ :error
tuple pair.
Examples
iex> BSV.VarInt.decode(<<253, 4, 1>>)
{:ok, 260}
iex> BSV.VarInt.decode(<<254, 0, 225, 245, 5>>)
{:ok, 100_000_000}
Specs
Decodes the given VarInt binary into an integer.
As decode/1
but returns the result or raises an exception.
Specs
Returns a binary of the length specified by the VarInt in the first bytes of the binary. Any remaining bytes are ignored.
Returns the result in an :ok
/ :error
tuple pair.
Examples
iex> BSV.VarInt.decode_binary(<<5, 104, 101, 108, 108, 111, 99, 99>>)
{:ok, "hello"}
Specs
Returns a binary of the length specified by the VarInt in the first bytes of the binary.
As decode_binary/1
but returns the result or raises an exception.
Specs
Encodes the given integer into a VarInt binary.
Examples
iex> BSV.VarInt.encode(260)
<<253, 4, 1>>
iex> BSV.VarInt.encode(100_000_000)
<<254, 0, 225, 245, 5>>
Specs
Prepends the given binary with a VarInt representing the length of the binary.
Examples
iex> BSV.VarInt.encode_binary("hello")
<<5, 104, 101, 108, 108, 111>>
Specs
Parses the given binary, returning a tuple with a binary of the length specified by the VarInt in the first bytes of the binary, and a binary of any remaining bytes.
Examples
iex> BSV.VarInt.parse_data(<<5, 104, 101, 108, 108, 111, 1, 2, 3>>)
{:ok, "hello", <<1, 2, 3>>}
Specs
Parses the given binary, returning a tuple with an integer decoded from the VarInt in the first bytes of the binary, and a binary of any remaining bytes.
Examples
iex> BSV.VarInt.parse_int(<<5, 104, 101, 108, 108, 111, 1, 2, 3>>)
{:ok, 5, <<104, 101, 108, 108, 111, 1, 2, 3>>}
Specs
parse_items(binary(), BSV.Serializable.t()) :: {:ok, [BSV.Serializable.t()], binary()} | {:error, term()}
Parses the given binary into a list of the length specified by the VarInt in
the first bytes of the binary. Each item is parsed according to the specified
Serializable.t/0
.