Binaries and Pattern Matching
Learn about pattern matching in binaries.
We'll cover the following
The first rule of binaries is, “If in doubt, specify the type of each field.” Available types are binary
, bits
, bitstring
, bytes
, float
, integer
, utf8
, utf16
, and utf32
. We can also add qualifiers:
size(n)
qualifies the size of the field, in bits.signed
andunsigned
qualifiy integer fields as signed or unsigned.- The endianness qualifiers are
big
,little
andnative
.
We use hyphens to separate multiple attributes of a field as follows: << length::unsigned-integer-size(12), flags::bitstring-size(4) >> = data
.
However, unless we’re doing a lot of work with binary file or protocol formats, the most common use of all this scary stuff is to process UTF-8 strings.
String processing with binaries
When we process lists, we use patterns that split the head
from the rest of the list. With binaries that hold strings, we can do the same kind of trick. We have to specify the type of the head
(UTF-8), and make sure the tail
remains a binary.
Below is an example that’ll produce the following result:
8706
111
103
Click the “Run” button to execute the code below.
Get hands-on with 1400+ tech skills courses.