我有地图:
%{ HEADER: "000000000 000000000000 00000000 ",
AM01: %{"C4" => "11111111", "C5" => "1"},
AM04: %{"C2" => "22222222", "C6" => "2"}
}
如何将地图映射成这样的字节:
"\x02\x1e000000000 000000000000 00000000 \x1e\x1cAM01\x1cC411111111\x1cC51\x1e\x1cAM04\x1cC222222222\x1cC62\x03"
到目前为止,我已经达到了这一点,但是最终结果是一个列表。
for segment <- %{AM03: %{"DB" => "1"}, AM04: %{"C2" => "1"}, AM06: %{"CR" => " "}}, into: [] do
{name, fields} = segment
for field <- fields, into: [] do
{field_name, field_value} = field
"\x1e\x1c#{name}\x1c#{field_name}\x1c#{field_value}"
end
end
我问:1-这是一种功能形式吗? 2-如何在此输出中提供Enum.join()?
答案 0 :(得分:3)
Kerenl.SpecialForm.for/1
理解支持开箱即用地收集到二进制文件中:
input = %{AM03: %{"DB" => "1"},
AM04: %{"C2" => "1"},
AM06: %{"CR" => " "}}
for {name, value} <- input, {fldn, fldv} <- value,
into: <<>>,
do: "\x1e\x1c#{name}\x1c" <> fldn <> "\x1c" <> fldv
#⇒ <<30, 28, 65, 77, 48, 51, ..., 82, 28, 32>>