ExBin v0.4.0 ExBin View Source

Documentation for ExBin.

Link to this section Summary

Functions

Returns the bit at a specific index. If the index exceeds the length of the bitstring, {:error, :index_out_of_bounds} is returned

Returns a "slice" of bits from a given bitstring, from range.first (inclusive) to range.last (exclusive)

Creates a stream for fetching bits from a bitstring in a lazily evaluated manner

Converts the given bitstr into a list of bits (integers)

Retrives the byte at index index (zero-based) from binary

Creates a stream for fetching bytes within a binary. Ideal for large volumes of bytes, or large binaries

Splits a bitstring into chunks of chunk_size bits each

Calculates the parity bit value of a given bitstring. Defaults to a even parity bit type which results in 0 if the number of 1's in the bitstring are even

Link to this section Functions

Returns the bit at a specific index. If the index exceeds the length of the bitstring, {:error, :index_out_of_bounds} is returned.

Examples

iex> ExBin.bit_at(<<0b1011 :: size(4)>>, 2)
1

iex> ExBin.bit_at(<<0b1011 :: size(4)>>, 10)
{:error, :index_out_of_bounds}
Link to this function

bit_slice(bitstr, range) View Source

Returns a "slice" of bits from a given bitstring, from range.first (inclusive) to range.last (exclusive).

If the range exceeds the length of the bitstring, the bits from range.first to the end of the bitstring will be returned.

Examples

iex> ExBin.bit_slice(<<0b10101::5>>, 0..3)
<<5::size(3)>>

iex> ExBin.bit_slice(<<0b10101::5>>, 2..10)
<<5::size(3)>>

Creates a stream for fetching bits from a bitstring in a lazily evaluated manner.

Examples

iex> ExBin.bit_stream(<<0xa5,0x93>>) |> Enum.take(16)
[1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1]

Converts the given bitstr into a list of bits (integers).

Examples:

iex> ExBin.bits(<<0xa5, 0x93>>)
[1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1]

Retrives the byte at index index (zero-based) from binary.

If the binary is empty, or already exhausted, byte_at will return {:error, :index_out_of_bounds}.

Examples

iex> ExBin.byte_at(<<0x0b, 0xc4, 0x3f>>, 1)
196

iex> ExBin.byte_at(<<0x0b, 0xc4, 0x3f>>, 7)
{:error, :index_out_of_bounds}

Creates a stream for fetching bytes within a binary. Ideal for large volumes of bytes, or large binaries.

Examples

iex> ExBin.byte_stream(<<0x01, 0x02, 0x03>>) |> Enum.take(2)
[1, 2]
Link to this function

chunks(bitstr, chunk_size) View Source
chunks(bitstring(), pos_integer()) :: Enumerable.t()

Splits a bitstring into chunks of chunk_size bits each.

Examples

iex> ExBin.chunks(<<0b1011000101111::size(13)>>, 3) |> Enum.to_list
[[1, 0, 1], [1, 0, 0], [0, 1, 0], [1, 1, 1], [1]]

iex> ExBin.chunks(<<0b10010111::size(8)>>, 4) |> Enum.to_list
[[1, 0, 0, 1], [0, 1, 1, 1]]
Link to this function

parity(bitstr, is_even \\ true) View Source
parity(bitstring(), boolean()) :: 0 | 1

Calculates the parity bit value of a given bitstring. Defaults to a even parity bit type which results in 0 if the number of 1's in the bitstring are even.

For odd parity bit type, set is_even to false; this will result in the inverse behavior.

Examples

iex> ExBin.parity(<<0b1001::4>>, true)
0

iex> ExBin.parity(<<0b1001::4>>, false)
1