gpt4 book ai didi

elixir - 二进制模式匹配到列表

转载 作者:行者123 更新时间:2023-12-02 06:16:39 24 4
gpt4 key购买 nike

我这样做:

<<a :: big-size(16), b :: big-size(16), c :: big-size(16)>> = <<0, 1, 0, 2, 0, 3>>

然后结果将是:

a = 1
b = 2
c = 3

但我真正需要的是:

a = [1, 2, 3]

有什么办法可以实现吗?

最佳答案

如果我正确阅读了你在 greggreg 的回答中的评论,这是一种方法:

<< size::8, rest::binary>> = <<3,0,25,1,1,2,1,6,4,3>>
<< data::size(size)-unit(16)-binary, rest::binary>> = rest
elements = for << <<element::16>> <- data>>, do: element

# At this point, elements is the list of n 16 bit integers
# (n being the first byte), and rest is the rest of the binary

关于elixir - 二进制模式匹配到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28506014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com