In Erlang, binary operation and matching are useful to develop programs for networking protocols. To write low-level codes to pack and unpack binary data at a bit level is easy and highly efficient.
Hit: As with strings, if the content of a binary is a printable string, then the shell will print the binary as a string; otherwise, it will be printed as a sequnce of integers.
@spec term_to_binary(Term) -> Bin
This converts any Erlang term into a binary.
The binary produced by term_to_binary is stored in the so-called external term format. Terms that have been converted to binaries by using term_to_binary can be stored in files, sent in messages over a network, and so forth, and the original term from which they were made can be reconstructed later. This is extremely useful for storing complex data structures in files or sending complex data structures to remote machines.
Bit Syntax Expressions
<<>>
<<E1,...,En>>
Each element Ei specifies a segment of the bit string. Each element Ei is a value, followed by an optional size expression and an optional type specifier list.
Ei = Value |
Value:Size |
Value/TypeSpecifierList |
Value:Size/TypeSpecifierList
Used in a bit string construction, Value is an expression which should evaluate to an integer, float or bit string. If the expression is something else than a single
literal or variable, it should be enclosed in parenthesis.
Used in a bit string matching, Value must be a variable, or an integer, float or string.
Note that, for example, using a string literal as in <<"abc">> is syntactic sugar for <<$a,$b,$c>>.
Used in a bit string construction, Size is an expression which should evaluate to an integer.
Used in a bit string matching, Size must be an integer or a variable bound to an integer.
The value of Size specifies the size of the segment in units (see below). The default value depends on the type (see below). For integer it is 8, for float it is 64,
for binary and bitstring it is the whole binary or bit string. In matching, this default value is only valid for the very last element. All other bit string or binary
elements in the matching must have a size specification.
TypeSpecifierList is a list of type specifiers, in any order, separated by hyphens (-). Default values are used for any omitted type specifiers.
Type= integer | float | binary | bytes | bitstring | bits
The default is integer, bytes is a shorthand for binary and bits is a shorthand for bitstring
Signedness= signed | unsigned
Only matters for matching and when the type is integer. The default is unsigned.
Endianness= big | little | native
Native-endian means that the endianness will be resolved at load time to be either big-endian or little-endian, depending on what is native for the CPU that the Erlang
machine is run on. Endianness only matters when the Type is either integer or float. The default is big.
Unit= unit:IntegerLiteral
The allowed range is 1..256. Defaults to 1 for integer, float and bitstring, and to 8 for binary.
The value of Size multiplied with the unit gives the number of bits. A segment of type binary must have a size that is evenly divisible by 8.
Note: When constructing binaries, if the size N of an integer segment is too small to contain the given integer, the most significant bits of the integer will be silently
discarded and only the N least significant bits will be put into the binary.
Examples:
1> Bin1 = <<1,17,42>>.
<<1,17,42>>
2> Bin2 = <<"abc">>.
<<97,98,99>>
3> Bin3 = <<1,17,42:16>>.
<<1,17,0,42>>
4> <<A,B,C:16>> = <<1,17,42:16>>.
<<1,17,0,42>>
5> C.
42
6> <<D:16,E,F>> = <<1,17,42:16>>.
<<1,17,0,42>>
7> D.
273
8> F.
42
9> <<G,H/binary>> = <<1,17,42:16>>.
<<1,17,0,42>>
10> H.
<<17,0,42>>
11> <<G,H/bitstring>> = <<1,17,42:12>>.
<<1,17,1,10:4>>
12> H.
<<17,1,10:4>>
Note that bit string patterns cannot be nested.