ExPcap.Binaries (expcap v0.1.2) View Source

This module provides utility functions for dealing with binaries.

Link to this section Summary

Functions

Reverses the bytes in the binary.

Reversed the contents of the first binary and prepends them to the second binary.

Converts a list of bytes to a binary.

Moves the contents of the list to the end of the binary.

Converts a binary to a hex representation.

Converts the first 32 bits of the binary to a signed integer.

Converts a binary to a list of bytes.

Moves the bytes from the binary to the list. The order of the bytes will be reversed until the degenerate case is reached.

Converts a binary to a 'raw' representation of the bytes.

Converts a binary to a string that shows the bytes in the binary.

Converts the first 16 bits of the binary to an unsigned integer.

Converts the first 32 bits of the binary to an unsigned integer.

Converts the first 4 bits of the binary to an unsigned integer.

Link to this section Functions

Specs

reverse_binary(binary()) :: binary()

Reverses the bytes in the binary.

Examples

iex> ExPcap.Binaries.reverse_binary(<<1, 2, 3, 4>>)
<<4, 3, 2, 1>>
Link to this function

reverse_binary(arg, acc)

View Source

Specs

reverse_binary(<<_::0>>, binary()) :: binary()
reverse_binary(binary(), binary()) :: binary()

Reversed the contents of the first binary and prepends them to the second binary.

This will recur until it reaches the degenerate case and returns the accumulator.

Examples

iex> ExPcap.Binaries.reverse_binary(<<3, 4>>, <<2, 1>>)
#<<3, 2, 1>>
#and then
<<4, 3, 2, 1>>

Specs

to_binary(list()) :: binary()

Converts a list of bytes to a binary.

Ideally, this would be replaced by a standard elixir function, but I have not been able to find such a function in the standard library.

Examples

iex> ExPcap.Binaries.to_binary([1, 2, 3, 4])
<<1, 2, 3, 4>>

Specs

to_binary([], binary()) :: binary()
to_binary(list(), binary()) :: binary()

Moves the contents of the list to the end of the binary.

This will recur until it reaches the degenerate case and returns the accumulator (binary).

Examples

iex> ExPcap.Binaries.to_binary([3, 4], <<1, 2>>)
#<<1, 2, 3>>
#and then
<<1, 2, 3, 4>>

Specs

to_hex(binary()) :: String.t()

Converts a binary to a hex representation.

This differs from 'Base.encode16' in that it adds the leading 0x prior to the hex value.

Note that the return type could be cleaned up here to only include 0-9 and a-f but no need to do that right now.

Examples

iex> ExPcap.Binaries.to_hex(<<255, 0>>)
"0xFF00"

Specs

to_int32(binary()) :: integer()

Converts the first 32 bits of the binary to a signed integer.

Examples

iex> ExPcap.Binaries.to_int32(<<255, 255, 255, 255>>)
-1

Specs

to_list(binary()) :: list()

Converts a binary to a list of bytes.

Examples

iex> ExPcap.Binaries.to_list(<<1, 2, 3, 4>>)
[1, 2, 3, 4]

Specs

to_list(<<_::0>>, [any()]) :: [any()]
to_list(binary(), list()) :: list()

Moves the bytes from the binary to the list. The order of the bytes will be reversed until the degenerate case is reached.

This will recur until it reaches the degenerate case and returns the accumulator (list).

Examples

iex> ExPcap.Binaries.to_list(<<3, 4>>, [2, 1])
#[3, 2, 1]
#and then
#[4, 3, 2, 1]
#and then
[1, 2, 3, 4]

Specs

to_raw(binary()) :: String.t()

Converts a binary to a 'raw' representation of the bytes.

Examples

iex> ExPcap.Binaries.to_raw(<<1, 2, 3, 4>>)
#<<1, 2, 3, 4>>
"... redacted ..."

Specs

to_string(binary()) :: String.t()

Converts a binary to a string that shows the bytes in the binary.

The typical display of a binary truncates the bytes, the intent here was to show the entire contents of the binary.

Examples

iex> ExPcap.Binaries.to_string(<<1, 2, 3, 4>>)
"<<1, 2, 3, 4>>"

Specs

to_uint16(binary()) :: non_neg_integer()

Converts the first 16 bits of the binary to an unsigned integer.

Examples

iex> ExPcap.Binaries.to_uint16(<<255, 255>>)
65535

Specs

to_uint32(binary()) :: non_neg_integer()

Converts the first 32 bits of the binary to an unsigned integer.

Examples

iex> ExPcap.Binaries.to_uint32(<<255, 255, 255, 255>>)
4294967295

Specs

to_uint4(binary()) :: non_neg_integer()

Converts the first 4 bits of the binary to an unsigned integer.

Examples

iex> ExPcap.Binaries.to_uint4(<<0xf :: size(4)>>)
15