包 erlang 的术语。构建一个二进制文件
Term to packet erlang. Constructing a binary
我有以下代码:
term_to_packet(Term) ->
B = term_to_binary(Term),
A = byte_size(B),
<< 1:4/integer-unit:8, B:A/integer-unit:8 >>.
然而,当我运行:
term_to_packet("Hello").
我收到一个错误:
exception error: bad argument in function term_to_packet line 20
第 20 行对应于 term_to_packet
函数的最后一行。
我不太确定是什么引发了这个错误。
B
是一个二进制,但在最后一行的二进制结构中,您指定它是一个 integer
。这似乎有效:
term_to_packet(Term) ->
B = term_to_binary(Term),
A = byte_size(B),
<< 1:4/integer-unit:8, B:A/binary-unit:8 >>.
我有以下代码:
term_to_packet(Term) ->
B = term_to_binary(Term),
A = byte_size(B),
<< 1:4/integer-unit:8, B:A/integer-unit:8 >>.
然而,当我运行:
term_to_packet("Hello").
我收到一个错误:
exception error: bad argument in function term_to_packet line 20
第 20 行对应于 term_to_packet
函数的最后一行。
我不太确定是什么引发了这个错误。
B
是一个二进制,但在最后一行的二进制结构中,您指定它是一个 integer
。这似乎有效:
term_to_packet(Term) ->
B = term_to_binary(Term),
A = byte_size(B),
<< 1:4/integer-unit:8, B:A/binary-unit:8 >>.