在Erlang中提取二进制文件的前N个元素

时间:2019-03-19 04:21:04

标签: split erlang

有人可以告诉我如何在Erlang的以下二进制列表中获取前4个字符:

<<245,185,198,200,52,227,138,93,250>>

3 个答案:

答案 0 :(得分:6)

使用Bit Syntax

<< R:4/binary,_/binary >> = <<245,185,198,200,52,227,138,93,250>>.

答案 1 :(得分:0)

有两种方法:

1)

将二进制文件拆分为列表-

[245,185,198,200,52,227,138,93,250] = binary_to_list(<<245,185,198,200,52,227,138,93,250>>).
{"õ¹ÆÈ",[52,227,138,93,250]} = lists:split(4, [245,185,198,200,52,227,138,93,250]).

2)

或获取直接二进制部分

<<"õ¹ÆÈ">> = binary:part(<<245,185,198,200,52,227,138,93,250>>, 0, 4).

3)

如果您需要精确的4个数字-您可以使用此功能

[A, B, C, D | _Tail] = binary_to_list(<<245,185,198,200,52,227,138,93,250>>).

2> A.
245
3> B.
185
4> C.
198
5> D

答案 2 :(得分:0)

1> M = <<245,185,198,200,52,227,138,93,250>>.
<<245,185,198,200,52,227,138,93,250>>
2> <<A,B,C,D, _/binary>> = M.
<<245,185,198,200,52,227,138,93,250>>
3> A.
245
4> B.
185
5> C.
198
6> D.
200
7>