Matching N digits using binaries
I have a packet coming from a socket which is composed by two subsections, the first composed by 4 numerical digits and the latter composed by 16 alphanumerica digits.
How can I bind the packet coming from the socket using binaries and pattern matching?
For example if I receive:
I would like to obtain:
Packet1 = 1234 Packet2 = "th58i4kr04o3"
You can do this by using Erlang bit syntax and binary pattern matching:
<<A:4/binary, Rest/binary>> = <<"1234th58i4kr04o3">>, Packet1 = binary_to_integer(A), Packet2 = binary_to_list(Rest).