Parsing null terminated strings in a binary.

Raimo Niskanen raimo@REDACTED
Thu Jan 13 08:50:03 CET 2005

Parse the binary in a loop something like this (untested):

z_split(<<B1:N/binary,0,B2/binary>>, N) ->
z_split(<<B1:N/binary>>, N) ->
z_split(<<_:N/binary,_/binary>>=B, N) ->
    z_split(B, N+1).

This will not build any sub-binaries until the zero is found.

cyberlync@REDACTED (Eric Merritt) writes:

> Guys,
>  I have a need to parse out a series of null terminated fields a
> binary. The easiest way to do this is to simply traverse the binary
> using bit syntax until a null byte occurs. However, I believe this
> would be less then efficient as a new binary would be created for each
> byte matched. Aside from created a C port to actually do the parsing
> do any of you have any ideas on how to do this efficiently?
> Thanks,
> Eric
> -- 
> I'm a programmer, I don't have to spell correctly; I just have to
> spell consistently


/ Raimo Niskanen, Erlang/OTP, Ericsson AB

More information about the erlang-questions mailing list