<<>>という特殊形式を使って定義します。
255より大きい整数を使った場合、::演算子で使用する単位やサイズを指定しない限り、最後の8ビットだけが使われます。<>/2を使うと、ビット列・バイナリ・文字列を連結できます。<<255>> == <<0xFF>>
# Overflowing bits are truncated
<<256>> == <<0>>
<<256::size(16)>> == <<1, 0>>
<<"Hello">> == <<72, 101, 108, 108, 111>>
_ヌルバイト_は<<0>>の別名です。
# 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>>
# Ignore the first 8 bytes, bind the remaining variable sized binary to `body`
<<_::binary-size(8), body::binary>>
これは文字列でも同じようにできます。
# 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"}