[erlang-questions] Offset calculation in bit syntax

Hynek Vychodil <>
Mon Nov 26 14:08:39 CET 2007


There is bug in your code I think.

Correct code should be:

decode(<<L:8/unsigned, R/binary>>) ->
  Len =  (L - 16#20)*8,
  <<Rest:Len/unsigned, _/binary>> = R,
  Rest.

--
--Hynek (Pichi) Vychodil

On 11/22/07, Ben Hood <> wrote:
>
>
> Hi,
>
> I have to parse the following byte sequence where the first byte indicates
> the payload length if the
> payload length in 15 bytes or less, e.g. the 3 byte payload
>
> x01 x02 x03
>
> will be encoded as
>
> 0x23 x01 x02 x03
>
> with 0x23 - 0x20 = 3, the length of the payload.
>
> So when I parse it, I'd like to use a bit syntax similar to following:
>
>
> decode(<< Len:8/unsigned, Rest:(Len - 16#20)/unsigned>>) ->
>  <<Rest>>.
>
> But this is an illegal bit size.
>
> Is there an elegant way around this?
>
> Thx,
>
> Ben
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list