Bite v0.1.1 Bite

A byte string convenience library.

Link to this section Summary

Functions

Consumes a binary as a ~b() sigil

Drop n bytes from a binary. Results in a binary. Tail recursive for binary inputs

Create a byte from an integer

Pad a binary until it’s length matches n

Reverse a binary

Consume a binary as a ~b() using the b sigil

Take n bytes from a binary and consume it into a bite. Not tail recursive

Convert a bite into an integer

Convert a bite to a string

Link to this section Types

Link to this type t()
t() :: %Bite{
  base: pos_integer(),
  bytes: binary(),
  endian: :big | :little,
  opts: charlist(),
  source: binary()
}

Link to this section Functions

Link to this function consume(binary, flags \\ [])
consume(binary(), charlist()) :: t()

Consumes a binary as a ~b() sigil.

Allows the flags:

  • ?l - interperate the bytes as little endian; if ?l is not specified, the number is assumed to be big endian
  • ?h - interperate the bytes as hexadecimal

Examples

iex> Bite.consume("0a 00 00 00", 'lh')
~b(0a 00 00 00)lh
iex> "b0 00 00 00" |> Bite.consume('lh') |> Bite.to_integer()
176
Link to this function drop(binary, n)
drop(binary(), integer()) :: binary()
drop(t(), integer()) :: t()

Drop n bytes from a binary. Results in a binary. Tail recursive for binary inputs.

In a bite, drops the n highest order bits respecting the endianness.

Examples

iex> "message" |> Bite.drop(2)
"ssage"
iex> "message" |> Bite.drop(20)
""
iex> ~b(6d 65 73 73 61 67 65)h |> Bite.drop(2) |> Bite.to_string()
"ssage"
Link to this function from_integer(n, acc \\ <<>>)

Create a byte from an integer

Link to this function pad_length(bytes, n, pad \\ <<0>>)

Pad a binary until it’s length matches n

Link to this function reverse(bytes, acc \\ <<>>)

Reverse a binary

Link to this macro sigil_b(arg, opts) (macro)
sigil_b({atom(), any(), [binary()]}, charlist()) :: t()

Consume a binary as a ~b() using the b sigil.

Uses consume/2 under the hood. See consume/2 for information on flags.

Examples

iex> import Bite, only: [sigil_b: 2]
iex> ~b(0a 00 00 00)hl
~b(0a 00 00 00)hl
Link to this function take(binary, n, opts \\ [])
take(binary(), integer(), charlist()) :: t()

Take n bytes from a binary and consume it into a bite. Not tail recursive.

Examples

iex> "ffffffff" |> Bite.take(4, [?h])
~b(ffff)h
iex> "ff" |> Bite.take(4, [?h])
~b(ff)h
iex> "ffff0000" |> Bite.take(4, 'hl')
~b(ffff)hl
Link to this function to_integer(bite)
to_integer(t()) :: integer()

Convert a bite into an integer.

Examples

iex> ~b(b0 00 00 00)hl |> Bite.to_integer()
176
Link to this function to_string(binary)
to_string(
  %Bite{
    base: term(),
    bytes: term(),
    endian: term(),
    opts: term(),
    source: term()
  }
  | String.t()
) :: String.t()

Convert a bite to a string.

Examples

iex> "6d 65 73 73 61 67 65" |> Bite.to_string()
"6d 65 73 73 61 67 65"
iex> ~b(6d 65 73 73 61 67 65)h |> Bite.to_string()
"message"
iex> ~b(6d 65 73 73 61 67 65)hl |> Bite.to_string()
"egassem"