Binary v0.0.2 Binary

Functions to operate on binaries.

Summary

Functions

Returns byte at given position. Numbering starts with 0

Create a binary with the binary content repeated n times

Returns the first byte of the binary as an integer

Convert list of bytes into binary

Returns the last byte of the binary as an integer

Pad with the provided byte at the beginning of the binary until provided length is achieved

Pad end of the binary with the provided byte until provided length is achieved

Reverse bytes order in the binary

Splits a binary into two at the specified position. Returns a tuple

Converts binary to a list of bytes

Removes all spcefied leading bytes from the binary

Removes all specified trailing bytes from the the binary

Functions

at(binary, postion)
at(binary, integer) :: byte

Returns byte at given position. Numbering starts with 0.

Position can be negative to make it relative to the end of the binary.

Returns nil if position is outside the binary (following Enum and String behavior)

Examples

iex> <<1, 2, 3>> |> Binary.at(1)
2
iex> <<1, 2, 3>> |> Binary.at(3)
nil
iex> <<1, 2, 3>> |> Binary.at(-1)
3
copy(bin, n)
copy(binary, non_neg_integer) :: binary

Create a binary with the binary content repeated n times.

first(bin)
first(binary) :: byte

Returns the first byte of the binary as an integer.

from_list(list)
from_list(list) :: binary

Convert list of bytes into binary.

last(bin)
last(binary) :: byte

Returns the last byte of the binary as an integer.

pad_leading(binary, len, byte \\ 0)
pad_leading(binary, non_neg_integer, byte) :: binary

Pad with the provided byte at the beginning of the binary until provided length is achieved.

pad_trailing(binary, len, byte \\ 0)
pad_trailing(binary, non_neg_integer, byte) :: binary

Pad end of the binary with the provided byte until provided length is achieved.

Examples

iex> <<3, 7>> |> Binary.pad_trailing(5)
<<3, 7, 0, 0, 0>>
reverse(binary)
reverse(binary) :: binary

Reverse bytes order in the binary.

split_at(binary, position)
split_at(binary, integer) :: byte

Splits a binary into two at the specified position. Returns a tuple.

When position is negative it’s counted from the end of the binary.

Examples

iex> <<1, 2, 3>> |> Binary.split_at(1)
{<<1>>, <<2, 3>>}
iex> <<1, 2, 3, 4>> |> Binary.split_at(-1)
{<<1, 2, 3>>, <<4>>}
iex> <<1, 2, 3>> |> Binary.split_at(10)
{<<1, 2, 3>>, <<>>}
to_list(bin)
to_list(binary) :: list

Converts binary to a list of bytes.

trim_leading(binary, byte \\ 0)
trim_leading(binary, byte) :: binary

Removes all spcefied leading bytes from the binary.

trim_trailing(binary, byte \\ 0)
trim_trailing(binary, byte) :: binary

Removes all specified trailing bytes from the the binary.

Examples

iex> <<0, 1, 2, 0, 0>> |> Binary.trim_trailing
<<0, 1, 2>>
iex> <<1, 2>> |> Binary.trim_trailing(2)
<<1>>