[erlang-questions] unexpected result of term_to_binary

Joe Armstrong <>
Mon Jan 21 21:26:10 CET 2013


On Mon, Jan 21, 2013 at 8:38 PM, Steve Davis <
> wrote:

> The following appears to encode lists of integers as strings (?!?):
>
>
No no no  - Erlang doesn't have strings - there are no strings in Erlang
(repeat this 100 times).

There are two internal types commonly used to represent strings
lists and binaries.

There are also string literals (ie things in programs like "aaaaabbbbb")
these are
just shorthand for lists of integers.

term_to_binary(Term) converts Term to a Binary (Bin). You're not supposed
to know whats *in* the binary. All you need to know is that
binary_to_term(Bin) is
the inverse.

It's like the bits in a float - you're not supposed to know what the bits
mean

99.999999% of all programs should not know what the *internal* structure
of the thing returned by  term_to_binary is (if you want to know it's the
term represented in the external format (
http://erlang.org/doc/apps/erts/erl_ext_dist.html)

Just about the *only* reason to call term_to_binary is that at some later
time
somebody will call binary_to_term on the result.



> Eshell V5.9.3  (abort with ^G)
> 1> term_to_binary([1,2,3,4]).
> <<131,107,0,4,1,2,3,4>>
>
> Is there a reason for this that I'm missing?
>

<<131,107,...>>

Is *not* a string - it's a binary literal

2> binary_to_term(<<131,107,0,4,1,2,3,4>>).
[1,2,3,4]

Cheers

/Joe




>
> regs,
> /s
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130121/3ecb1ca0/attachment.html>


More information about the erlang-questions mailing list