Erlang – Binaries

Erlang binaries

In this guide, we will discuss Erlang Binaries. Use a data structure called a binary to store large quantities of raw data. Binaries store data in a much more space efficient manner than in lists or tuples, and the runtime system is optimized for the efficient input and output of binaries.

Binaries are written and printed as sequences of integers or strings, enclosed in double less than and greater than brackets.

Following is an example of binaries in Erlang −

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   io:fwrite("~p~n",[<<5,10,20>>]), 
   io:fwrite("~p~n",[<<"hello">>]).

When we run the above program, we will get the following result.

Output

<<5,10,20>>
<<"hello">>

Let’s look at the Erlang functions which are available to work with Binaries −

Sr.No.Methods & Description
1list_to_binary
This method is used to convert an existing list to a list of binaries.
2split_binary
This method is used to split the binary list based on the index position specified.
3term_to_binary
This method is used to convert a term to binary.
4is_binary
This method is used to check if a bitstring is indeed a binary value.
5binary_part
This method is used to extract a part of the binary string
6binary_to_float
This method is used to convert a binary value to a float value.
7binary_to_integer
This method is used to convert a binary value to a integer value.
8binary_to_list
This method is used to convert a binary value to a list.
9binary_to_atom
This method is used to convert a binary value to an atom.

Next Topic : Click Here

This Post Has One Comment

Leave a Reply