The library
The decode and encode functions use a buffer and an index into the buffer, which points at the point where to encode and decode. The index is updated to point right after the term encoded/decoded. No checking is done whether the term fits in the buffer or not. If encoding goes outside the buffer, the program can crash.
All functions take two parameters:
The data is thus at
All encode functions assume that the
There are also encode functions that use a dynamic buffer. It
is often more convenient to use these to encode data. All encode
functions comes in two versions; those starting with
All functions return
Some of the decode functions need a pre-allocated buffer. This
buffer must be allocated large enough, and for non-compound types
the
typedef enum {
ERLANG_ASCII = 1,
ERLANG_LATIN1 = 2,
ERLANG_UTF8 = 4
} erlang_char_encoding;
The character encodings used for atoms.
Decodes an atom from the binary format. The
Decodes an atom from the binary format. The
The wanted string encoding is specified by
This function fails if the atom is too long for the buffer
or if it cannot be represented with encoding
This function was introduced in Erlang/OTP R16 as part of a first step to support UTF-8 atoms.
Decodes an integer in the binary format to a GMP
Decodes a binary from the binary format. Parameter
Decodes a boolean value from the binary format.
A boolean is actually an atom,
Decodes a char (8-bit) integer between 0-255 from the binary format.
For historical reasons the returned integer is of
type
Decodes a double-precision (64-bit) floating point number from the binary format.
Decodes any term, or at least tries to. If the term
pointed at by
The function returns
The
Decodes a fun from the binary format. Parameter
Decodes a list header from the binary
format. The number of elements is returned in
Notice that lists are encoded as strings if they consist
entirely of integers in the range 0..255. This function do
not decode such strings, use
Decodes a long integer from the binary format.
If the code is 64 bits, the function
Decodes a GCC
Decodes a map header from the binary
format. The number of key-value pairs is returned in
Decodes a process identifier (pid) from the binary format.
Decodes a port identifier from the binary format.
Decodes a reference from the binary format.
Decodes a string from the binary format. A
string in Erlang is a list of integers between 0 and
255. Notice that as the string is just a list, sometimes
lists are encoded as strings by
The string is copied to
Decodes a term from the binary format. The term
is return in
Notice that this function is located in the
Decodes an Erlang trace token from the binary format.
Decodes a tuple header, the number of elements
is returned in
Decodes an unsigned long integer from the binary format.
If the code is 64 bits, the function
Decodes a GCC
Decodes the version magic number for the Erlang binary term format. It must be the first token in a binary term.
Encodes an atom in the binary format. Parameter
Encodes an atom in the binary format with character encoding
The encoding fails if
These functions were introduced in Erlang/OTP R16 as part of a first
step to support UTF-8 atoms. Atoms encoded with
Encodes a GMP
Encodes a binary in the binary format. The data is at
Encodes a boolean value as the atom
Encodes a char (8-bit) as an integer between 0-255 in the binary
format. For historical reasons the integer argument is of
type
Encodes a double-precision (64-bit) floating point number in the binary format.
Returns
Encodes an empty list. It is often used at the tail of a list.
Encodes a fun in the binary format. Parameter
Encodes a list header, with a specified
arity. The next
For example, to encode the list
ei_encode_list_header(buf, &i, 3); ei_encode_atom(buf, &i, "c"); ei_encode_atom(buf, &i, "d"); ei_encode_list_header(buf, &i, 1); ei_encode_atom(buf, &i, "e"); ei_encode_atom(buf, &i, "f"); ei_encode_empty_list(buf, &i);
It may seem that there is no way to create a list without
knowing the number of elements in advance. But indeed
there is a way. Notice that the list
To encode a list, without knowing the arity in advance:
while (something()) { ei_x_encode_list_header(&x, 1); ei_x_encode_ulong(&x, i); /* just an example */ } ei_x_encode_empty_list(&x);
Encodes a long integer in the binary format.
If the code is 64 bits, the function
Encodes a GCC
Encodes a map header, with a specified arity. The next
For example, to encode the map
ei_x_encode_map_header(&x, 2); ei_x_encode_atom(&x, "a"); ei_x_encode_string(&x, "Apple"); ei_x_encode_atom(&x, "b"); ei_x_encode_string(&x, "Banana");
A correctly encoded map cannot have duplicate keys.
Encodes an Erlang process identifier (pid) in the binary
format. Parameter
Encodes an Erlang port in the binary format. Parameter
Encodes an Erlang reference in the binary format. Parameter
Encodes a string in the binary format. (A string in Erlang
is a list, but is encoded as a character array in the binary
format.) The string is to be
Encodes an
Encodes an Erlang trace token in the binary format.
Parameter
Encodes a tuple header, with a specified
arity. The next
For example, to encode the tuple
ei_encode_tuple_header(buf, &i, 2); ei_encode_atom(buf, &i, "a"); ei_encode_tuple_header(buf, &i, 2); ei_encode_atom(buf, &i, "b"); ei_encode_tuple_header(buf, &i, 0);
Encodes an unsigned long integer in the binary format.
If the code is 64 bits, the function
Encodes a GCC
Encodes a version magic number for the binary format. Must be the first token in a binary term.
Returns the type in
Prints a term, in clear text, to the file
specified by
In
The return value is the number of characters written to the file
or string, or
Argument
By default, the
A call to
If this function is called, it can only be called once
and must be called before any other functions in the
You can run into trouble if this feature is used carelessly. Always ensure that all communicating components are either from the same Erlang/OTP release, or from release X and release Y where all components from release Y are in compatibility mode of release X.
Skips a term in the specified buffer; recursively skips elements of lists and tuples, so that a full term is skipped. This is a way to get the size of an Erlang term.
This can be useful when you want to hold arbitrary terms: skip them and copy the binary term data to some buffer.
Returns
Appends data at the end of buffer
Formats a term, given as a string, to a buffer.
Works like a sprintf for Erlang terms.
~a An atom, char* ~c A character, char ~s A string, char* ~i An integer, int ~l A long integer, long int ~u A unsigned long integer, unsigned long int ~f A float, float ~d A double float, double float ~p An Erlang pid, erlang_pid*
For example, to encode a tuple with some stuff:
ei_x_format("{~a,~i,~d}", "numbers", 12, 3.14159) encodes the tuple {numbers,12,3.14159}
Frees an
Allocates a new
Some tips on what to check when the emulator does not seem to receive the terms that you send: