[erlang-questions] Unexpected list_to_binary behavior

Sean Cribbs <>
Tue Mar 18 15:23:52 CET 2014


integer_to_binary/1 could be written as integer_to_binary(N) ->
list_to_binary(integer_to_list(N)). Furthermore, what you ask for is not
straightforward, given the numerous encodings of integers available. What
if I wanted a 64-bit signed little-endian integer encoded in the binary?
You can't tell i2b that, but you could do it with bit syntax:
 <<65535:64/little-signed>>  ( <<0,0,1,0,0,0,0,0>> ).


On Tue, Mar 18, 2014 at 9:15 AM, Valentin Micic <>wrote:

> Yes, you're right and this make sense at some level, particularly if the
> function I've called was
>
> characters_to_binary/1.
>
> Indeed, as Magnus Henoch mentioned in a related email, there is a function
> for conversion of unicode characters:
>
> unicode:characters_to_binary/1;
>
> So, why should the other one be called: list_to_binary/1 instead instead
> of characters_to_binary/1
>
> If one disregard the existence of any previous knowledge, and given that a
> list may contain any number of arbitrary types, including a list
> itself,  what should be a reasonable expectation for (and here's the kicker
> -- not an input argument itself, for it is perfectly acceptable to assume
> it to be a list, but) a content of the input argument?
>
> It's surprising (at least was to me) to expect a content of an input
> argument ( formerly known as *list *) to be limited to a list of octets,
> as much as it is surprising to see that  integer_to_binary( 65536 ) results
> in <<"65536">> instead of <<255,255>>, that is, surprising to se that it
> behaves like a shortcut to  *list_to_binary( integer_to_list( 65536 )).*
>
> On the other hand, it is what it is, and, in a way,  I am quite
> embarrassed that I assumed that integers would be automagically converted
> to appropriate number of octets, for this is just as unreasonable and
> arbitrary expectation.
>
> Thank you for your responses.
>
> Kind regards
>
> V/
>
>
> On 18 Mar 2014, at 3:19 PM, Steve Vinoski wrote:
>
>
>
>
> On Tue, Mar 18, 2014 at 9:13 AM, Valentin Micic <>wrote:
>
>> Hi all,
>>
>> (my apologies if this has been covered already)
>>
>> Is there any reason why:
>>
>> *()820> list_to_binary( lists:seq(1,255) ).*
>> *<<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,*
>> *  22,23,24,25,26,27,28,29,…>>*
>>
>> 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,
>>                                   23,24,25,26,27,28|…])
>>
>> 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?
>>
>
> This has nothing to do with the number of characters, but rather that you
> can't represent the value 256 in a byte:
>
> 1> list_to_binary([256]).
> ** exception error: bad argument
>      in function  list_to_binary/1
>         called as list_to_binary([256])
>
> --steve
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>


-- 
Sean Cribbs <>
Software Engineer
Basho Technologies, Inc.
http://basho.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140318/fb8fa06a/attachment.html>


More information about the erlang-questions mailing list