[erlang-questions] Binary Parsing
wolfmanjm
wolfmanjm@REDACTED
Sun Mar 22 04:52:30 CET 2009
Oops my bad the other posts didn't show up in the google mirror, I
only saw the one that said the problem was Length being unbound, which
it is, as I do that, however I have never tried using an expression
and that apparently is not allowed, kind of a pity.
On Mar 21, 8:35 pm, wolfmanjm <wolfma...@REDACTED> wrote:
> Actually Length is bound as it is bound in the preceding Expression,
> so what you are doing is valid I do it all the time, I think what you
> need to do is put parenthesis around...
>
> (Att:Length - 2)/binary
>
> as mentioned in the docs...
>
> "Used in a bit string construction, Value is an expression which
> should evaluate to an integer, float or bit string. If the expression
> is something else than a single literal or variable, it should be
> enclosed in parenthesis."
>
> On Mar 21, 12:52 pm, Pete Stapley <pstap...@REDACTED> wrote:
>
> > I am attempting to parse some Radius attributes with Erlang and I have
> > run into a bit of a problem. I pass some binary data to parseAttributes
> > and I want to start peeling off the attributes one by one. I first peel
> > off Type which is one byte, then Length which is also one byte. Next I
> > want to peel off the Attribute. The Length variable is the length of the
> > attribute plus the Type and Length byte, so I have to subtract 2 bytes
> > from the Length variable before peeling off the attribute. I have tried
> > the method below without any luck. Is there a way to do this?
>
> > Thanks,
>
> > -define(BYTE, 8/unsigned-big-integer).
>
> > parseAttributes(Bin) ->
> > parseAttributes(Bin, []).
>
> > parseAttributes(<<>>, Atts) ->
> > {ok, Atts};
> > %%problem is here, Length - 2 does not seem to work
> > parseAttributes( <<Type:?BYTE, Length:?BYTE, Att:Length - 2/binary,
> > Rest/binary>>, Atts) ->
> > io:format("Type: ~p Length: ~p Att:~p Length: ~p~n", [Type, Length,
> > Att, size(Rest)]),
> > parseAttributes(Rest, Atts).
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questi...@REDACTED://www.erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questi...@REDACTED://www.erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions
mailing list