My BERT is passed to Erlang via the query string. I read it through gen_tcp with the http_bin parameter so that it acts like this: <131,104,1,100,0,2,104,105 "→. This is almost correct because I want to decode it with binary_to_term / 2. But binary_to_term / 2 wants binary binary. not a binary file (it wants <<131,104,1,100,0,2,104,105 → not <<131,104,1,100,0,2,104,105 "→).
I can make it out in the correct form.
parse(Source) ->
Bins = binary:split(Source, <<",">>, [global]),
parse(Bins, []).
parse([H | T], Acc) ->
parse(T, [list_to_integer(binary_to_list(H)) | Acc]);
parse([], Acc) ->
list_to_binary(lists:reverse(Acc)).
But this seems confusing and slower than I had hoped for (~ 5k / sec, each of which is 200 bytes). Also came up with something based on io_lib: fread / 2, but it wasn’t much better and still looks awkward.
source
share