(erl@localhost)6> list_to_binary([1, [2], 3]).<br><<1,2,3>><br>(erl@localhost)7> binary_to_list(list_to_binary([1, [2], 3])).<br>[1,2,3]<br>(erl@localhost)8><br><br>But I see you are using iolist, so you are probably not interested in nested lists keeping their structure.<br>
<br><div class="gmail_quote">On Thu, Feb 9, 2012 at 11:51 AM, Loïc Hoguin <span dir="ltr"><<a href="mailto:essen@ninenines.eu">essen@ninenines.eu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hm I would be very surprised if binary_to_list(list_to_binary(<u></u>L)) /= L wouldn't be true. Do you have an example of this?<div class="im"><br>
<br>
On 02/09/2012 12:48 PM, Robert Raschke wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
Sure, but don't you usually use them in pairs? Just wondering cause<br>
list_to_binary/1 and binary_to_list/1 are not opposites of each other,<br>
that is, binary_to_list(list_to_binary(<u></u>L)) /= L in general. But I'm<br>
guessing that probably doesn't matter for your use case.<br>
<br>
On Thu, Feb 9, 2012 at 11:31 AM, Loïc Hoguin <<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a><br></div><div class="im">
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>>> wrote:<br>
<br>
Those encode the term in the Erlang term format, which has a very<br>
particular purpose. The functions I'm talking about are just<br>
conversion from one type to binary strings.<br>
<br>
<br>
On 02/09/2012 12:29 PM, Robert Raschke wrote:<br>
<br>
Are you guys sure you're not looking for term_to_binary/1 and<br>
binary_to_term/1 ?<br>
<br>
On Thu, Feb 9, 2012 at 10:27 AM, Loďc Hoguin <<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a><br>
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>><br></div><div class="im">
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a> <mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>>>> wrote:<br>
<br></div><div><div class="h5">
While seemingly practical, I'm not sure why I would want any<br>
place<br>
in my code where I wouldn't be sure what the input type is. Even<br>
with dialyzer checks I'm sure I would make false assumptions<br>
and get<br>
surprises here and there.<br>
<br>
I also would like an efficient solution, both in processing<br>
speed<br>
and memory use. Converting to list then binary isn't that great,<br>
even less so after module:function calls with guards.<br>
<br>
<br>
On 02/09/2012 09:03 AM, Rapsey wrote:<br>
<br>
I wrote functions that convert between all types that I use<br>
constantly.<br>
Much easier to just force a specific type somewhere if<br>
you're<br>
not sure<br>
what the input type is going to be instead of doing checks<br>
everywhere or<br>
annoying conversions like the one you wrote.<br>
It has a dependency on mochinum:digits/1 though.<br>
<br>
tolist(<<_/binary>> = P) -><br>
binary_to_list(P);<br>
tolist(P) when is_atom(P) -><br>
atom_to_list(P);<br>
tolist(P) when is_integer(P) -><br>
integer_to_list(P);<br>
tolist(P) when is_float(P) -><br>
mochinum:digits(P);<br>
tolist(P) when is_list(P) -><br>
P.<br>
<br>
tobin(<<_/binary>> = P) -><br>
P;<br>
tobin(P) when is_list(P) -><br>
iolist_to_binary(P);<br>
tobin(P) when is_atom(P) -><br>
atom_to_binary(P,latin1);<br>
tobin(P) when is_integer(P) -><br>
tobin(integer_to_list(P));<br>
tobin(P) when is_float(P) -><br>
tobin(mochinum:digits(P)).<br>
<br>
toatom(P) when is_binary(P) -><br>
binary_to_atom(P,latin1);<br>
toatom(P) when is_list(P) -><br>
list_to_atom(P);<br>
toatom(P) when is_atom(P) -><br>
P.<br>
toint(<<_/binary>> = P) -><br></div></div>
list_to_integer(binary_to_____<u></u>list(P));<div class="im"><br>
<br>
toint([_|_] = P) -><br>
list_to_integer(P);<br>
toint(P) when is_integer(P) -><br>
P;<br>
toint(P) when is_float(P) -><br>
erlang:round(P).<br>
tofloat(P) when is_integer(P) -><br>
P / 1;<br>
tofloat(P) when is_float(P) -><br>
P;<br>
tofloat(P) when is_binary(P) -><br>
tofloat(binary_to_list(P));<br>
tofloat(P) when is_list(P) -><br></div>
Str = string:join(string:tokens(P,",<u></u>____"),"."),<div class="im"><br>
<br>
case string:str(Str,".") of<br>
0 -><br>
tofloat(P ++ ".0");<br>
_ -><br>
list_to_float(Str)<br>
end;<br>
tofloat(P) -><br>
list_to_float(tolist(P)).<br>
<br>
<br>
On Wed, Feb 8, 2012 at 7:29 PM, Loďc Hoguin<br>
<<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a> <mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>><br>
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a> <mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>>><br>
<br>
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a> <mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>><br>
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a> <mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>>>>> wrote:<br>
<br>
Hello,<br>
<br>
It feels a bit tedious to keep doing things like<br></div>
list_to_binary(integer_to_____<u></u>__list(42))<br>
<br>
or even<br>
list_to_binary(atom_to_list(__<u></u>____forty_two))<div class="im"><br>
<br>
<br>
<br>
when we could have convenient integer_to_binary/1 or<br>
/2 or<br>
atom_to_list/1 BIFs.<br>
<br>
Is there any chance to add them in a future release?<br>
With<br>
all the<br>
advantages of binaries it's a shame it's not easier<br>
to deal<br>
with them.<br>
<br>
Thanks.<br>
<br>
--<br>
Loďc Hoguin<br>
Erlang Cowboy<br>
Nine Nines<br></div>
______________________________<u></u>_______________________<div class="im"><br>
<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a> <mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@<u></u>erlang.org</a>><br>
<mailto:<a href="mailto:erlang-questions@" target="_blank">erlang-questions@</a>__<a href="http://erlang.org" target="_blank">erl<u></u>ang.org</a><br>
<mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@<u></u>erlang.org</a>>><br></div>
<mailto:<a href="mailto:erlang-questions@" target="_blank">erlang-questions@</a><br>
<mailto:<a href="mailto:erlang-questions@" target="_blank">erlang-questions@</a>>__<a href="http://erl__ang.org" target="_blank">er<u></u>l__ang.org</a> <<a href="http://erlang.org" target="_blank">http://erlang.org</a>><br>
<mailto:<a href="mailto:erlang-questions@" target="_blank">erlang-questions@</a>__<a href="http://erlang.org" target="_blank">erl<u></u>ang.org</a><br>
<mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@<u></u>erlang.org</a>>>><br>
<a href="http://erlang.org/mailman/______listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/____<u></u>__listinfo/erlang-questions</a><br>
<<a href="http://erlang.org/mailman/____listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/___<u></u>_listinfo/erlang-questions</a>><div class="im"><br>
<<a href="http://erlang.org/mailman/____listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/___<u></u>_listinfo/erlang-questions</a><br>
<<a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a>>><br>
<br>
<<a href="http://erlang.org/mailman/____listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/___<u></u>_listinfo/erlang-questions</a><br>
<<a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a>><br>
<<a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a><br>
<<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a>>>><br>
<br>
<br>
<br>
<br>
--<br>
Loďc Hoguin<br>
<br>
Erlang Cowboy<br>
Nine Nines<br>
______________________________<u></u>_____________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a> <mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@<u></u>erlang.org</a>><br>
<mailto:<a href="mailto:erlang-questions@" target="_blank">erlang-questions@</a>__<a href="http://erlang.org" target="_blank">erl<u></u>ang.org</a><br>
<mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@<u></u>erlang.org</a>>><br>
<a href="http://erlang.org/mailman/____listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/____<u></u>listinfo/erlang-questions</a><br>
<<a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a>><br>
<<a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a><br>
<<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a>>><br>
<br>
<br>
<br>
<br>
______________________________<u></u>___________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a> <mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@<u></u>erlang.org</a>><br>
<a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a><br>
<<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a>><br>
<br>
<br>
<br></div><div class="im">
--<br>
Loďc Hoguin<br>
Erlang Cowboy<br>
Nine Nines<br>
<br>
<br>
<br>
<br></div><div class="im">
______________________________<u></u>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a><br>
</div></blockquote><span class="HOEnZb"><font color="#888888">
<br>
<br>
-- <br>
Loïc Hoguin<br>
Erlang Cowboy<br>
Nine Nines<br>
</font></span></blockquote></div><br>