在《erlang程序设计》第五章中有如下说明:
@spec term_to_binary(term) -> bin
该函数可以将任何 erlang 项式转化为相应的二进制数据。而转换得到的二进制数据是以所谓的“外部数据格式”存储的。这种数据可以用于文件存储、网络传输等。而且在转换之后,还可以从这些二进制数据中还原出原始的数据项。该函数在需要对复杂的数据结构进行文件存储和网络传输时极为有用。
在《erts-x.x.x》的 reference manual -> erlang 中有如下说明:
<a href="http://my.oschina.net/moooofly/blog/281986#">?</a>
1
2
3
4
5
6
7
<code>term_to_binary(term) -> ext_binary()</code>
<code>types:</code>
<code> </code><code>term = term()</code>
<code>returns a binary data object</code><code>which</code> <code>is the result of encoding term according to the erlang external term</code><code>format</code><code>.</code>
<code>根据 erlang 外部项式格式对 term 进行编码,返回相应的二进制数据对象</code>
<code>this can be used</code><code>for</code> <code>a variety of purposes,</code><code>for</code> <code>example writing a term to a</code><code>file</code> <code>in</code> <code>an efficient way, or sending</code>
<code>an erlang term to some</code><code>type</code> <code>of communications channel not supported by distributed erlang.</code>
erlang:term_to_binary/1,2 函数返回值是 erlang 扩展 term 格式 (erlang external term format) 的 binary,即 ext_binary() 。 这个函数能把 erlang 数据封装成二进制流,是一种存储和传输 erlang 数据的有效途径,甚至可以用这种封包/解包方式用作 socket 的通信协议(某页游项目就是这么干的)。