Back to Library
Region:
Switch to ID
Intermediate Exercism • elixir
Binaries
Lesson Overview
# Introduction
About
- The binary type is a specialization of the bitstring type.
- Binaries are made up of bytes.
- Bytes are 8 bits.
- Bytes can represent numbers from
0to255 - Hexadecimal integer values are common when working with bytes:
0x00to0xFF.
- Binary literals are defined using the
<<>>special form.- If you use an integer larger than
255for a byte, only the last 8 bits are used, unless you specify the unit and/or size to use using the::operator.
- If you use an integer larger than
<>/2can be used to concatenate bitstrings/binaries/strings.
<<255>> == <<0xFF>>
# Overflowing bits are truncated
<<256>> == <<0>>
<<256::size(16)>> == <<1, 0>>
<<"Hello">> == <<72, 101, 108, 108, 111>>
A null-byte is another name for <<0>>.
Strings are encoded binary data
- Strings are encoded binary data in UTF-8 format.
- They are encoded in 8-bit (one-byte) chunks, using up to 4 bytes to represent more than 255 characters.
- The length of a string may not be the same as its byte size.
# This string has a string length of 5, but is made up of 7 bytes
string = "cześć"
String.length(string) != byte_size(string)
# Even emoji strings are encoded binaries
"👍" == <<240, 159, 145, 141>>
Pattern Matching on binary data
- Binaries can be pattern matched as a whole or using the special form to match just one part.
- Only the last portion in a pattern match may have a variable size.
# Ignore the first 8 bytes, bind the remaining variable sized binary to `body`
<<_::binary-size(8), body::binary>>
This can be also done for strings:
# bind the first 5 bytes to `name`,
# match the string literal " the ",
# bind remaining bytes to `species`
<<name::binary-size(5), " the ", species::binary>> = <<"Frank the Walrus">>
{name, species}
# => {"Frank", "Walrus"}
Originally from Exercism elixir concepts