Erlang/Elixir 为什么这个是错的

wudixiaotie · 2014年12月03日 · 最后由 jsvisa 回复于 2014年12月08日 · 2848 次阅读
<<1:8/binary>>.

这种写法为什么报错?

(node5@erlang)27> <<1:8/binary>>. ** exception error: bad argument

(node8@erlang)33> <> = list_to_binary("abcdefgh"). <<"abcdefgh">> (node8@erlang)34> <> = list_to_binary("abcdefg"). ** exception error: no match of right hand side value <<"abcdefg">> (node8@erlang)35>

(node6@erlang)109> <<(erlang:integer_to_binary(1))/binary>>. <<"1">> (node6@erlang)110> <<(erlang:integer_to_binary(1)):8/binary>>. ** exception error: bad argument (node6@erlang)111> <<(erlang:integer_to_binary(1)):1/binary>>. <<"1">> (node6@erlang)112> <<(erlang:integer_to_binary(1)):2/binary>>. ** exception error: bad argument

综上所述,你的写法有问题

如果你想用二进制取值你用 2 楼的方法,如果是构建二进制用 3 楼的 <<(erlang:integer_to_binary(1)):1/binary>>.

(node6@erlang)116> <<1/integer>>. <<1>> (node6@erlang)117> <<1>>.
<<1>> (node6@erlang)118> <<1:8>>. <<1>> (node6@erlang)119> <<1:83333>>. <<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,...>> (node6@erlang)120> <<1:22>>.
<<0,0,1:6>> (node6@erlang)121> <<1:23>>. <<0,0,1:7>> (node6@erlang)122> <<1:234>>. <<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,...>> (node6@erlang)123> <<1:14>>. <<0,1:6>> (node6@erlang)124> <<1:145>>. <<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1:1>> (node6@erlang)125> <<1:0>>.
<<>> (node6@erlang)126> <<1:1>>. <<1:1>> (node6@erlang)127> <<1:2>>. <<1:2>> (node6@erlang)128> <<1:3>>. <<1:3>> (node6@erlang)129> <<1:4>>. <<1:4>> (node6@erlang)130> <<1:5>>. <<1:5>> (node6@erlang)131> <<1:6>>. <<1:6>> (node6@erlang)132> <<1:7>>. <<1:7>> (node6@erlang)133> <<1:8>>. <<1>> (node6@erlang)134> <<1:>>.

  • 1: syntax error before: '>>' (node6@erlang)134> <<1:9>>. <<0,1:1>> (node6@erlang)135> <<1:10>>. <<0,1:2>> (node6@erlang)136> <<1:11>>. <<0,1:3>> (node6@erlang)137> <<1:12>>. <<0,1:4>> (node6@erlang)138> <<1:13>>. <<0,1:5>> (node6@erlang)139> <<1:14>>. <<0,1:6>> (node6@erlang)140> <<1:15>>. <<0,1:7>> (node6@erlang)141> <<1:16>>. <<0,1>> (node6@erlang)142> <<1:32>>. <<0,0,0,1>> (node6@erlang)143> <<1:64>>. <<0,0,0,0,0,0,0,1>> (node6@erlang)144>

This representation is known as the abstract format. Functions dealing with such parse trees are compile:forms/[1,2] and functions in the modules epp, erl_eval, erl_lint, erl_pp, erl_parse, and io. They are also used as input and output for parse transforms (see the module compile).

1 是个数字,只有 8 位,你要匹配 8 个 binary, 64 位就报错了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号