I frequently deal with "binary" protocols that exchange information using some type of COMMAND|LENGTH|PARAMETERS structure where PARAMETERS is any number of TAG|LENGTH|VALUE tuples. Erlang makes short work of extracting values in the message with pattern matching, like:
M = <<1, 4, 1, 2, 16#abcd:16>>.
<<1,4,1,2,171,205>>
With the M bitstring (a message following the COMMAND|LENGTH|PARAMETERS format), I can utilize Erlang pattern matching to extract the Command, Length, Parameters:
<<Command:8,Length:8,Parameters/binary>> = M.
<<1,4,1,2,171,205>>
Parameters.
<<1,2,171,205>>
For managing "bit-nibble-byte"-oriented protocols this is invaluable!
Do any other languages come close to supporting syntax like this, even through an add-on library?
Something like https://pypi.python.org/pypi/bitstring/3.1.3 for python allows you to do a lot of work at this same level.
From your example:
from bitstring import BitStream
M = BitStream('0x01040102abcd')
[Command, Length, Parameters] = M.readlist('hex:8, hex:8, bits')
gives Parameters
as BitStream('0x0102abcd')
.