[erlang-questions] binary expression with fixed and variable fields
Thu Jul 1 14:18:33 CEST 2010
On Tue, Jun 29, 2010 at 7:22 PM, info <> wrote:
> Hi all,
> I have a string composed of several fixed and variable fields. I would like
> to parse this string as binary expression.
> I checked the example with an IP datagram (the variable fields are to the
> end ...):
> If the size3 of the field3 is variable,how to parse the string ?
Fields Size must be determined before the match or within the match.
> <<field1:size1,field2:size2,Rest/binary>> but after ?
> If a field is empty, how to detect it ?
Use another binary expression.
case Bin of
_ -> % catch all others
> If a char (e.g comma) is between each field, is it always possible to use
> the binary syntax ?
Just use the "," notation:
Long binary strings are used whenever the binary holds enough information
when you need to "parse" and not "match" you may use smaller binary strings.
Taken from some kind of code of mine:
-define( UINT32(X), X:32/native-unsigned).
-define( UINT16(X), X:16/native-unsigned).
action(5522146, _Function, <<
?UINT32(Cy)>>, Pid) ->
%io:format("~p: ~s, ~p ~p ~p~n", [?MODULE, Function, Pid, Hwnd, Title ]),
my 2 cents.
More information about the erlang-questions