ExPcap.Binaries
This module provides utility functions for dealing with binaries.
Summary↑
reverse_binary(b) | Reverses the bytes in the binary |
reverse_binary(arg1, acc) | Reversed the contents of the first binary and prepends them to the second binary |
to_binary(list) | Converts a list of bytes to a binary |
to_binary(list, acc) | Moves the contents of the list to the end of the binary |
to_hex(b) | Converts a binary to a hex representation |
to_int32(b) | Converts the first 32 bits of the binary to a signed integer |
to_list(b) | Converts a binary to a list of bytes |
to_list(b, acc) | Moves the bytes from the binary to the list. The order of the bytes will be reversed until the degenerate case is reached |
to_raw(b) | Converts a binary to a ‘raw’ representation of the bytes |
to_string(b) | Converts a binary to a string that shows the bytes in the binary |
to_uint16(b) | Converts the first 16 bits of the binary to an unsigned integer |
to_uint32(b) | Converts the first 32 bits of the binary to an unsigned integer |
to_uint4(b) | Converts the first 4 bits of the binary to an unsigned integer |
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>>
Specs:
- reverse_binary(binary, binary) :: binary
- reverse_binary(<<_ :: 0>>, 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(list, binary) :: binary
- to_binary([], 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(binary, list) :: list
- to_list(<<_ :: 0>>, [any]) :: [any]
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