[erlang-questions] unexpected result of term_to_binary
Mon Jan 21 21:41:07 CET 2013
On Mon, Jan 21, 2013 at 8:46 PM, Paul Davis <paul.joseph.davis@REDACTED>wrote:
> In Erlang a list of integers in the range 0-255 is a string.
No it's not - a list of integers in the range 0-255 is not a string - it's
a list of integers
from 0-255 and nothing else - you don't a clue what it represents.
> Ages = [65,66,67].
is a list of the ages (integers) at which many people become pensioners in
Sweden - it's also three ascii characters codes for 'A' 'B' and 'C'.
All erlang knows is that this is a list of three integers it hasn't a clue
what it means.
The shell prints this as "ABC" (this is a string literal representing the
integers [65,66,67]) This is because the shell prints a list of printable
character codes as a string literal, ie it guesses that the intended
meaning is as a list of character codes and prints it as a string to help
In this case the shell guessed wrong. It's a list of ages :-)
We can use io:format to change the printed representation
> io:format(" ~s ~w ~p~n", [Ages, Ages,Ages]).
ABC [65,66,67] "ABC"
Ages is a list of three integers.
> An easy test is to show that the value 256 changes the encoding into a
> list of integers:
> > term_to_binary([1, 2, 3, 4, 256]).
> On Mon, Jan 21, 2013 at 1:38 PM, Steve Davis <
> steven.charles.davis@REDACTED> wrote:
>> The following appears to encode lists of integers as strings (?!?):
>> Eshell V5.9.3 (abort with ^G)
>> 1> term_to_binary([1,2,3,4]).
>> Is there a reason for this that I'm missing?
>> erlang-questions mailing list
> erlang-questions mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions