[erlang-questions] Terms to binary and viceversa

Björn-Egil Dahlberg wallentin.dahlberg@REDACTED
Thu Jan 17 01:10:53 CET 2013


Pids are not always 27 bytes. Names are atoms.

Example:

-module(t).

-compile([export_all]).

start() ->
    Pid = self(),
    PidBin = term_to_binary(Pid),
    SomeData = <<"some other data">>,
    Bin = <<$A, PidBin/binary, SomeData/binary>>,
    <<$A, 131, 103, 100, Len:16, Name:Len/binary, Id:4/binary,
Serial:4/binary, Creation, Rest/binary>> = Bin,
    DecPid = binary_to_term(<<131, 103, 100, Len:16, Name/binary,
Id/binary, Serial/binary, Creation>>),
    {Pid, DecPid, Rest}.

Output:

egil@REDACTED ~ $ erl -sname some-node-name-used
Erlang R15B (erts-5.9) [source] [smp:4:4] [async-threads:0] [hipe]
[kernel-poll:false]

Eshell V5.9  (abort with ^G)
(some-node-name-used@REDACTED)1> t:start().
{<0.37.0>,<0.37.0>,<<"some other data">>}


// Björn-Egil


2013/1/17 Fred Hebert <mononcqc@REDACTED>

> The pattern:
>
>     <<$A, BinPid:27/binary, Data/binary>>
>
> Should do it. Then you can get `Pid` by calling `binary_to_term/1` on
> `BinPid`. By default, the size of the `binary` type of matching is in
> bytes, so you should be extracting it fine. To get it in bits, you'd
> need to specify `BinPid:216/binary-unit:1`, for example.
>
> In your case, what you were doing is returning `Pid` as a 216 bits
> integer (the default type), not something you can use in any useful way.
>
> Regards,
> Fred.
>
> On 01/17, Alessandro Sivieri wrote:
> > Hi all,
> >
> > I have the following situation: two processes are exchanging data, and
> one
> > particular packet contains the following three contents:
> >
> >    - a character (which defines the type of that specific packet, in this
> >    case it is a 'A')
> >    - a pid
> >    - some binary data which I encode and decode in a specific way
> >
> > The question is: how can I extract the pid part using the bit syntax? I
> am
> > able to extract the character, and I know that the pid (encoded to binary
> > with term_to_binary) is 27 bytes, or 216 bits, so my first thought was
> > something like
> >
> > <<$A:8, Pid:216, Data/binary>>
> >
> > But this is wrong because in Pid at this point I have a very large
> integer
> > (and I see my error in that pattern), so do I have to get each single
> byte
> > of the 27 bytes composing the pid one by one or is there a pattern that
> > immediately matches that term giving me something that I can pass to
> > binary_to_term? I have experimented a little but I always obtain a no
> > matching clause...
> >
> > Alessandro
> >
> > --
> > Sivieri Alessandro
> > alessandro.sivieri@REDACTED
> > http://sivieri.wordpress.com/
>
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130117/735d7b20/attachment.htm>


More information about the erlang-questions mailing list