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
t() :: %Bite{ base: pos_integer(), bytes: binary(), endian: :big | :little, opts: charlist(), source: binary() }
Link to this section Functions
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
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"
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.
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
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
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"