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 ?
If all messages have a 4 byte length header, I’d recommend using
erlang:decode_packet(Type,Bin,Options)whereTypeis set to4. This will return{ok, Message, Rest}whereMessageis your first message andRestis 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.