View Source Tezex.Zarith (tezex v2.0.0)

A Zarith number is an integer encoded as a variable length sequence of bytes.

Each byte has a running unary size bit:

  • the most significant bit of each byte tells if this is the last byte in the sequence (0) or if there is more to read (1)
  • the second most significant bit of the first byte is reserved for the sign (positive if zero).

Size and sign bits ignored, data is then the binary representation of the absolute value of the number in little endian order.

Summary

Functions

Consumes a zarith-encoded integer. Takes a binary and returns the decoded integer in base 10 along with how many characters of the input binary were used to decode the integer.

Decodes a zarith-encoded integer. Takes a binary and returns the integer in base 10.

Encodes an integer to a zarith-encoded binary. Takes an integer in base 10 and returns the encoded binary.

Functions

@spec consume(nonempty_binary()) :: {%{int: binary()}, pos_integer()}

Consumes a zarith-encoded integer. Takes a binary and returns the decoded integer in base 10 along with how many characters of the input binary were used to decode the integer.

@spec decode(nonempty_binary()) :: integer()

Decodes a zarith-encoded integer. Takes a binary and returns the integer in base 10.

Examples

iex> decode("a1d22c")
365_729

iex> decode("e1d22c")
-365_729
@spec encode(integer()) :: nonempty_binary()

Encodes an integer to a zarith-encoded binary. Takes an integer in base 10 and returns the encoded binary.

Implementation based on anchorageoss/tezosprotocol (MIT License - Copyright (c) 2019 Anchor Labs, Inc.)

Examples

iex> encode(365_729)
"a1d22c"

iex> encode(-365_729)
"e1d22c"