I have a binary <<"a,b,c">> and I would like to extract the information from this binary.
So I would like to have something like A=a, B=b and so on.
I need a general approach on this because the binary string always changes.
So it could be <<"aaa","bbb","ccc">>…
I tried to generate a list
erlang:binary_to_list(<<"a","b","c">>)
but I get string as a result.
"abc"
Thank you.
You did use the right method.
There is no string type in Erlang: http://www.erlang.org/doc/reference_manual/data_types.html#id63119. The console just displays the lists in string representation as a courtesy, if all elements are in printable ASCII range.
You should read Erlang’s “Bit Syntax Expressions” documentation to understand how to work on binaries.
Do not convert the whole binary into a list if you don’t need it in list representation!
To extract the first three bytes you could use
If you want to iterate over the binary data, you can use binary comprehension.
Pattern matching is possible, too:
Even for reading one UTF-8 character at once. So to convert a binary UTF-8 string into Erlang’s “list of codepoints” format, you could use:
(Note that
`<<"aaa","bbb","ccc">> = <<"aaabbbccc">>. Don’t actually use the last code snipped but the linked method.)