在 Erlang 中使用十六进制
Using Hexadecimal in Erlang
有什么方法可以将十六进制字符列表转换为与十六进制编码对应的二进制
例子:
[FF,AC,01]=><<255,172,1>>
我猜你是这个意思:["FF","AC","01"] => <<255,172,1>>
。
您可以使用list_to_integer/2功能。它以数字为基数作为第二个参数。
Hexs = ["FF","AC","01"],
Ints = [list_to_integer(Hex, 16) || Hex <- Hexs],
%% [255,172,1]
Binary = list_to_binary(Ints).
%% <<255,172,1>>
已接受答案的替代方法是通过二进制理解直接转到二进制:
1> Hexs = ["FF","AC","01"].
2> << <<(list_to_integer(C,16)):8>> || C <- Hexs >>.
<<255,172,1>>
有什么方法可以将十六进制字符列表转换为与十六进制编码对应的二进制 例子: [FF,AC,01]=><<255,172,1>>
我猜你是这个意思:["FF","AC","01"] => <<255,172,1>>
。
您可以使用list_to_integer/2功能。它以数字为基数作为第二个参数。
Hexs = ["FF","AC","01"],
Ints = [list_to_integer(Hex, 16) || Hex <- Hexs],
%% [255,172,1]
Binary = list_to_binary(Ints).
%% <<255,172,1>>
已接受答案的替代方法是通过二进制理解直接转到二进制:
1> Hexs = ["FF","AC","01"].
2> << <<(list_to_integer(C,16)):8>> || C <- Hexs >>.
<<255,172,1>>