One more question?

Steve Davis <>
Sat Feb 13 11:04:30 CET 2010


"Awesome" is indeed how I would describe binary matching in Erlang.

I first discovered the full beauty of it when I was faced with
decoding UDP packets from a rather idiosynchratic protocol. Breaking
out the bitflags and integer values of the header of these packets
turned out to be as simple as:

decode(<<Zerocoded:1, Reliable:1, Resent:1, Ack:1, _Unused:4, Sequence:
32, Extra, Content/binary>>) ->
    .....

As well as being highly efficient, the Erlang code adds semantic value
by allowing you to directly name things for what they are without a
bunch of preamble/enforced declarations.

/s



On Feb 12, 11:10 pm, Jayson Vantuyl <> wrote:
> If you receive it via a streaming port, you can also use {packet,4} as an option, which will automatically break the binaries into appropriate lengths.
>
> Also, one of the more unusual (but awesome) bits of binary pattern matching is something like this:
>
> > <<Bytes:32/integer, Chunk:Bytes/binary>>
>
> Interestingly, you can also do it like this, to match chunks in a large binary:
>
> > <<Bytes:32/integer, Chunk:Bytes/binary, Rest/binary>>
>
> On Feb 12, 2010, at 12:03 PM, Ish Rattan wrote:
>
>
>
> > Erlang server gets message from non-Erlang client as:
> > <<0,0,0,10,72,101,108,108,111,46,46>>
> > (numeric 10 in 4-bytes followed by "hello..")
>
> > how to extract the pieces?
>
> > -ishwar
>
> > ________________________________________________________________
> > erlang-questions (at) erlang.org mailing list.
> > Seehttp://www.erlang.org/faq.html
> > To unsubscribe; mailto:
>
> --
> Jayson Vantuyl
> 
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> Seehttp://www.erlang.org/faq.html
> To unsubscribe; mailto:


More information about the erlang-questions mailing list