[erlang-questions] bit syntax feature request
Sun Jul 29 00:06:56 CEST 2007
I knew I had seen this request before:
Some interesting questions are raised in the thread.
2007/7/28, Serge Aleynikov <>:
> When parsing some protocols I run into cases when I needed to convert
> parts of a binary to strings (i.e. lists). Unfortunately current bit
> syntax expressions don't support Value:Size/string or Value:Size/list types:
> Ei = Value |
> Value:Size |
> Value/TypeSpecifierList |
> Type = integer | float | binary
> The default is integer.
> I need to parse an integer, and string from a binary:
> 1> <<X:16/integer, S1:3/binary, _/binary>> = <<1,2,$a,$b,$c,$d,3,4>>.
> 2> S = binary_to_list(S1).
> what I'd like to be able to do is:
> 1> <<X:16/integer, S:3/string, _/binary>> = <<1,2,$a,$b,$c,$d,3,4>>.
> 2> io:format("~p, ~p\n", [X, S]).
> 258, "abc".
> When most parts of the binary are strings, it becomes quite verbose to
> introduce many binary_to_list/1 calls and diminishes the value of
> pattern matching syntax.
> Any particular reason this is not currently implemented? If not, how
> hard would it be to add such a feature?
> erlang-questions mailing list
More information about the erlang-questions