> I have a binary file <<"a,b,c">>and I would like to extract information from this bi...">

Erlang how to make a list from this binary << "a, b, c" >>

I have a binary file <<"a,b,c">>and I would like to extract information from this binary file.

So I would like to have something like A=a, B=band so on. I need a general approach to this, because the binary string is always changing.

So it could be <<"aaa","bbb","ccc">>...

I tried to create a list

erlang:binary_to_list(<<"a","b","c">>) 

but I get the string as a result.

"abc"

Thank.

+7
source share
1 answer

You used the correct method.

binary_to_list (binary) โ†’ [char ()]

Returns a list of integers that correspond to bytes of a binary file.

Erlang : http://www.erlang.org/doc/reference_manual/data_types.html#id63119. , ASCII.

Erlang " " , , .

, !

,

<<A, B, C, Rest/binary>> = <<"aaa","bbb","ccc">>.

, .

<< <<(F(X))>> || <<X>> <= <<"aaa","bbb","ccc">> >>.

:

test(<<A, Tail/binary>>, Accu) -> test(Tail, Accu+A);
test(_, Accu) -> Accu.

882 = test(<<"aaa","bbb","ccc">>, 0).

UTF-8 . UTF-8 Erlang " " , :

test(<<A/utf8, Tail/binary>>, Accu) -> test(Tail, [A|Accu]);
test(_, Accu) -> lists:reverse(Accu).

[97,97,97,600,99,99,99] = test(<<"aaa", 16#0258/utf8, "ccc">>, "").

( , `<<"aaa","bbb","ccc">> = <<"aaabbbccc">>. , .)

+15

All Articles