Show Menu
Cheatography

Erlang Binaries Cheat Sheet by

Overview

Erlang can match binaries just as any list of things. <<E1, E2, E3>> = Bin divides the binary Bin into three elements of type integer (which is default) of one byte each. This means that Bin has to be 24 bits long, or we get a badma­tch. You can also make partial matches, in a [Head | Tail] fashion, by putting /bits­tring on the last element, like so: <<E1, E2, E3/bit­str­ing­>> = Bin. This is a type modifier and tells Erlang that there are two 8-bit elements, in E1 and E2 respec­tively, and then an undete­rmined amount of bits stored in E3.

Type Modifiers

Type
Size in bits
Rema­rks
integer
8
Default type
float
64
binary| bytes
8 per chunk
Anything matched must be of size evenly divisible by 8
bitstring| bits
1 per chunk
Will always match
utf8|­utf16| utf32
8-32, 16-32, and 32
<<­"­abc­"­/ut­f8>­> is the same as <<­$a/­utf8, $b/utf8, $c/utf­8>>

Other Type Modifiers

Kinds
Values
Rema­rks
Signedness
signed|unsigned
Unsigned is default
Endianness
big|little|native
native is resolved at load time to whatever the CPU uses
Custom unit
unit:IntLiteral
Define a custom unit of length 1..256
 

Segments

Each segment in a binary has the following general syntax: Value:Size/TypeSpecifierList. The Size or the TypeSpecifier, or both, can be omitted.

Value is either a literal or a variable, Size is multiplied by the unit in TypeSpecifierList, and can be any expression that evaluates to an integer.
Cont­rived example: <<­X:4­/li­ttl­e-s­ign­ed-­int­ege­r-u­nit­:8>­> has a total size of 4*8 = 32 bits, and it contains a signed integer in little endian byte order.

Examples

Expr­ess­ion
Result
<<­"­abc­"­>>
<<­97,­98,­99>­>
<<­1,1­7,4­2:1­6>>
<<­1,1­7,0­,42­>>
<<­A,B­,C:­16>> = <<1­,17­,42­:16­>>
C = 42
<<­D:1­6,E­,F>> = <<1­,17­,42­:16­>>
D = 273 and F = 42
<<­G,R­est­/bi­nar­y>> = <<1­,17­,42­:12­>>
<<­1,1­7,0­,42­>>
<<­G,R­est­/bi­tst­rin­g>> = <<1­,17­,42­:12­>>
<<­1,1­7,2­,10­:4>­>
<<­Int­AsL­itt­leE­ndi­an>> =
<<IntAsBigEndian/little>>
IntAs­Big­Endian = 9494 and
IntAsLittleEndian = 22
<<­"­pöp­cör­n"/u­tf8­>>
This is how Erlang handles unicode
When constr­ucting a binary, if the size of an integer N is too large to fit inside the given segment, the most signif­icant bits are silently discarded and only the N least signif­icant bits kept.

Binary Compre­hension Examples

Just like with lists, there is a notation for binary comprehension. It works almost the same, you just exchange the <- before the generator for a <=. Here is an example of how to use this to convert a 32 bit integer into a hex representation:

int_as_hex(Int) ->
    IntAsBin = <<Int:32>>,
    "0x" ++ lists:flatten([byte_to_hex(<<Byte>>) || <<Byte:8>> <= IntAsBin]).

byte_to_hex(<<Nibble1:4, Nibble2:4>>) ->`
    [integer_to_list(Nibble1, 16), integer_to_list(Nibble2, 16))].
Depending on what you put around the compre­hension expres­sion, the end result is either a binary, or as above, a list.
       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          PHP Syntax for beginners Cheat Sheet
          DokuWiki Cheat Sheet
          C Reference Cheat Sheet