Sunday, June 10, 2007

Shrink or Strip a binary octet-stream easily

I've found a simple way to suppress ending characters of a Binary without parsing it or change it into a list !

I wanted to remove those <<"\r\n">> characters from lines read from a file, and know that my line are made of 29 characters (reading date strings).

So with this function:

shrinkbin(Data, Size) ->
<<Data:Size/binary-unit:8>>.


I can do:

node> test:shrinkbin(<<"Sun Jun 10 15:20:53 CEST 2007\r\n">>, 29).
<<"Sun Jun 10 15:20:53 CEST 2007">>.


That's it !

1 comment:

Kärnkraft för klimatet said...

I think You need to capitalize size in the example.

shrinkbin(Data, Size) ->
<<Data:Size/binary-unit:8>>.

Sticky