Syntax 数据包erlang的术语。构造二进制

Syntax 数据包erlang的术语。构造二进制,syntax,erlang,Syntax,Erlang,我有以下代码: term_to_packet(Term) -> B = term_to_binary(Term), A = byte_size(B), << 1:4/integer-unit:8, B:A/integer-unit:8 >>. 我得到一个错误: exception error: bad argument in function term_to_packet line 20 其中,第20行对应于术语到\u数据包的最后一

我有以下代码:

term_to_packet(Term) ->
    B = term_to_binary(Term),   
    A = byte_size(B),
    << 1:4/integer-unit:8, B:A/integer-unit:8 >>.
我得到一个错误:

exception error: bad argument in function term_to_packet line 20
其中,第20行对应于
术语到\u数据包的最后一行


我不太确定是什么引发了这个错误。

B
是一个二进制,但在最后一行的二进制结构中,您指定它是一个
整数。这似乎有效:

term_to_packet(Term) ->
B = term_to_binary(Term),   
A = byte_size(B),
<< 1:4/integer-unit:8, B:A/binary-unit:8 >>.
术语到数据包(术语)->
B=术语到二进制(术语),
A=字节大小(B),
>.

No,对于二进制,单位默认为8位。(对于整数,它默认为1位。)
term_to_packet(Term) ->
B = term_to_binary(Term),   
A = byte_size(B),
<< 1:4/integer-unit:8, B:A/binary-unit:8 >>.