[erlang-questions] Terms to binary and viceversa

Fred Hebert mononcqc@REDACTED
Thu Jan 17 00:38:35 CET 2013

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.


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

More information about the erlang-questions mailing list