Telegram TL v0.2.1 TL.Binary

A few methods extending erlang’s :binary module.

Summary

Functions

Split a binary at the given index

Build an integer from a list of bit positions

Converts the binary representation (of a signed integer) to its decimal representation

Converts a (signed) integer to its binary representation

Reverse the endianness of a binary

Functions

binary_split(binary, index)

Split a binary at the given index.

Example

iex> TL.Binary.binary_split <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>, 3
{<<1, 2, 3>>, <<4, 5, 6, 7, 8, 9, 10>>}
build_integer_from_bits_list(list, value \\ 0)

Build an integer from a list of bit positions.

Example

iex> TL.Binary.build_integer_from_bits_list([0,1,3,10])
1035 # = 2^0 + 2^1 + 2^3 + 2^10
decode_signed(binary)

Converts the binary representation (of a signed integer) to its decimal representation.

Examples

iex> TL.Binary.decode_signed <<2, 154>>
666
iex> TL.Binary.decode_signed <<253, 102>>
-666
encode_signed(int)

Converts a (signed) integer to its binary representation.

Examples

iex> TL.Binary.encode_signed 666
<<2, 154>>
iex> TL.Binary.encode_signed -666
<<253, 102>>
reverse_endianness(bytes)

Reverse the endianness of a binary.

Example

iex> TL.Binary.reverse_endianness(<<1,2,3>>)
<<3,2,1>>