MQTT Variable Byte Integer encoding and decoding (1 to 4 bytes, max 268,435,455).
Summary
Functions
Decodes a Variable Byte Integer from the front of binary.
Encodes a non-negative integer (0..268_435_455) into a Variable Byte Integer binary.
Same as encode/1 but returns the binary directly or raises ArgumentError.
Functions
@spec decode(binary()) :: {:ok, non_neg_integer(), binary()} | :incomplete | {:error, :malformed_varint}
Decodes a Variable Byte Integer from the front of binary.
Returns:
{:ok, value, rest}on success:incompletewhen more bytes are needed{:error, :malformed_varint}when the encoding exceeds 4 bytes
@spec encode(non_neg_integer()) :: {:ok, binary()} | {:error, :varint_out_of_range}
Encodes a non-negative integer (0..268_435_455) into a Variable Byte Integer binary.
@spec encode!(non_neg_integer()) :: binary()
Same as encode/1 but returns the binary directly or raises ArgumentError.