A classical problem!<br><br>My preferred solution to this is to do it in steps:<br><br>parse(<<Type:8,FullLength:8,TempRest/binary>>) -><br> Length = FullLength - 2,<br> << Value:Length/binary,Rest/binary >> = TempRest,<br>
{Value,Rest}.<br><br>This approach has served us well, but I would have liked to be able to do this more directly but the language does not allow it.<br><br>Cheers,<br>Torben<br><br><div class="gmail_quote">On Sat, Mar 21, 2009 at 8:52 PM, Pete Stapley <span dir="ltr"><<a href="mailto:pstapley@rapidnet.com">pstapley@rapidnet.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I am attempting to parse some Radius attributes with Erlang and I have<br>
run into a bit of a problem. I pass some binary data to parseAttributes<br>
and I want to start peeling off the attributes one by one. I first peel<br>
off Type which is one byte, then Length which is also one byte. Next I<br>
want to peel off the Attribute. The Length variable is the length of the<br>
attribute plus the Type and Length byte, so I have to subtract 2 bytes<br>
from the Length variable before peeling off the attribute. I have tried<br>
the method below without any luck. Is there a way to do this?<br>
<br>
Thanks,<br>
<br>
-define(BYTE, 8/unsigned-big-integer).<br>
<br>
parseAttributes(Bin) -><br>
parseAttributes(Bin, []).<br>
<br>
parseAttributes(<<>>, Atts) -><br>
{ok, Atts};<br>
%%problem is here, Length - 2 does not seem to work<br>
parseAttributes( <<Type:?BYTE, Length:?BYTE, Att:Length - 2/binary,<br>
Rest/binary>>, Atts) -><br>
io:format("Type: ~p Length: ~p Att:~p Length: ~p~n", [Type, Length,<br>
Att, size(Rest)]),<br>
parseAttributes(Rest, Atts).<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br>