[erlang-questions] Unexpected list_to_binary behavior
Tue Mar 18 14:24:15 CET 2014
Valentin Micic <> writes:
> Hi all,
> (my apologies if this has been covered already)
> Is there any reason why:
> ()820> list_to_binary( lists:seq(1,255) ).
> works, but
> ()821> list_to_binary( lists:seq(1,256) ).
> ** exception error: bad argument
> in function list_to_binary/1
> called as list_to_binary([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,
> breaks on Erlang 16B02? Was the limit to 255 characters for list_to_binary/1 always there and I just failed to see it before?
The problem is not the length of the list, but the values in it:
2> list_to_binary(lists:duplicate(256, 1)).
list_to_binary just takes a list of byte values (0 =< n =< 255) and puts
them into a binary. (If you want to convert Unicode codepoints, which
may be greater than 255, you could use unicode:characters_to_binary/1.)
More information about the erlang-questions