Reputation: 2918
Let a binary string composed of messages separated by one null byte:
<message><null><message><null> ... <message><null>
I would like to split them. Easy, I do:
binary:split(Bin,<<0>>,[global]),
But ...
But one message is composed of two parts:
<length><texte>
length has a 4-bytes fixed size and the length can have null bytes !
Then the split function cannot cut correctly the string.
Does exist a way according to erlang state of art ?
Upvotes: 1
Views: 700
Reputation: 16577
If all messages have a 4 byte length header, I'd recommend using erlang:decode_packet(Type,Bin,Options)
where Type
is set to 4
. This will return {ok, Message, Rest}
where Message
is your first message and Rest
is the rest of the binary. Just rinse and repeate until you reach the end of the binary (you might have to take care of the null bytes yourself inbetween).
If, however, not all messages have a 4 byte length prefix and there's no deterministic way of detecting that header it is probably impossible to reliably parse such a list.
Upvotes: 5