如何在 Erlang 中连接两个二进制文件?

发布于 2024-07-14 21:31:36 字数 338 浏览 5 评论 0原文

如何在 Erlang 中连接两个二进制文件?

例如,假设我有:

B1 = <<1,2>>.
B2 = <<3,4>>.

如何连接 B1 和 B2 以创建二进制 B3,其为 <<1,2,3,4>>?

我问这个问题的原因是因为我正在编写代码来对某些网络协议的数据包进行编码。 我通过为数据包中的字段编写编码器来实现这一点,并且我需要连接这些字段以构建整个数据包。

也许我这样做的方式是错误的。 我应该将数据包构建为整数列表并在最后一刻将该列表转换为二进制吗?

How do I concatenate two binaries in Erlang?

For example, let's say I have:

B1 = <<1,2>>.
B2 = <<3,4>>.

How do I concatenate B1 and B2 to create a binary B3 which is <<1,2,3,4>>?

The reason I am asking this is because I am writing code to encode a packet for some networking protocol. I am implementing this by writing encoders for the fields in the packet and I need to concatenate those fields to build up the whole packet.

Maybe I am doing this the wrong way. Should I build up the packet as a list of integers and convert the list to a binary at the last moment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

‖放下 2024-07-21 21:31:36
28> B1= <<1,2>>.
<<1,2>>
29> B2= <<3,4>>.
<<3,4>>
30> B3= <<B1/binary, B2/binary>>.
<<1,2,3,4>>
31>
28> B1= <<1,2>>.
<<1,2>>
29> B2= <<3,4>>.
<<3,4>>
30> B3= <<B1/binary, B2/binary>>.
<<1,2,3,4>>
31>
爺獨霸怡葒院 2024-07-21 21:31:36

您可以使用位语法连接二进制文件:

1> B1 = <<1,2>>.
<<1,2>>
2> B2 = <<3,4>>.
<<3,4>>
3> B3 = <<B1/binary, B2/binary>>.
<<1,2,3,4>>

在许多情况下,特别是在数据发往网络的情况下,您可以通过构造 io_list 来避免连接。

B3 = [B1, B2],
gen_tcp:send(Socket, B3).

这是 O(1),因为它避免了复制任一二进制文件。 gen_tcp:send 将接受深度列表并遍历结构以进行输出。 (两个元素列表需要很少的额外内存,因此内存开销很小。)

在某些情况下(重复追加到同一个二进制文件),Erlang 现在进行了优化,避免复制要追加的二进制文件,因此使用 io_lists 可能不太相关: http://erlang.org/doc/efficiency_guide/binaryhandling.html#constructing -binaries


历史记录:我最初只建议使用 io_list 解决方案,很多评论者正确地指出我未能回答这个问题。 希望已接受的答案现已完成。 (11年后!)

You can concatenate binaries using bit syntax:

1> B1 = <<1,2>>.
<<1,2>>
2> B2 = <<3,4>>.
<<3,4>>
3> B3 = <<B1/binary, B2/binary>>.
<<1,2,3,4>>

In many cases, particularly where the data is destined for the network you can avoid the concatenation by constructing an io_list instead.

B3 = [B1, B2],
gen_tcp:send(Socket, B3).

This is O(1) as it avoids copying either binary. gen_tcp:send will accept the deep list and walk the structure for output. (A two element list takes very little additional memory, so the memory overhead is small.)

In some cases (repeated appends to the same binary), Erlang now has optimisations that avoid copying the binary being appended to so using io_lists may be less relevant: http://erlang.org/doc/efficiency_guide/binaryhandling.html#constructing-binaries


Historical note: I originally advised only the io_list solution and a lot of commenters correctly point out that I failed to answer the question. Hopefully, the accepted answer is now complete. (11 years later!)

烟沫凡尘 2024-07-21 21:31:36

要使用 io_list,您可以这样做:

erlang:iolist_to_binary([<<"foo">>, <<"bar">>])

这很好且清晰。 如果更方便的话,您还可以使用列表和其中的内容。

To use an io_list, you could do:

erlang:iolist_to_binary([<<"foo">>, <<"bar">>])

Which is nice and legible. You could also use lists and things in there if it's more convenient.

遮云壑 2024-07-21 21:31:36

以最后一个答案为基础:

bjoin(List) ->
    F = fun(A, B) -> <<A/binary, B/binary>> end,
    lists:foldr(F, <<>>, List).

To build on the last answer:

bjoin(List) ->
    F = fun(A, B) -> <<A/binary, B/binary>> end,
    lists:foldr(F, <<>>, List).
被你宠の有点坏 2024-07-21 21:31:36

使用erlang函数list_to_binary(List)你可以在这里找到文档:
http://www.erlang.org/documentation/doc-5.4.13/lib/kernel-2.10.13/doc/html/erlang.html#list_to_binary/1

use the erlang function list_to_binary(List) you can find the documentation here:
http://www.erlang.org/documentation/doc-5.4.13/lib/kernel-2.10.13/doc/html/erlang.html#list_to_binary/1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文