Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Erlang 生成32字符十六进制格式的随机16字节字符串_Erlang_Elixir - Fatal编程技术网

Erlang 生成32字符十六进制格式的随机16字节字符串

Erlang 生成32字符十六进制格式的随机16字节字符串,erlang,elixir,Erlang,Elixir,我不熟悉长生不老药/二郎。我已经使用Ruby一段时间了,我想把这段代码转换成Elixir/Erlang SecureRandom::random_bytes(16).each_byte.map { |b| sprintf("%02X",b) }.join %%生成随机字节 =加密:强随机字节(16)。 %%转换为十六进制 列表:展平(io_lib:format(“~32.16.0b”,[X]))。 要获得相同的结果,在Elixir中,您可以使用Erlang模块:crypto

我不熟悉长生不老药/二郎。我已经使用Ruby一段时间了,我想把这段代码转换成Elixir/Erlang

SecureRandom::random_bytes(16).each_byte.map { |b| sprintf("%02X",b) }.join
%%生成随机字节
=加密:强随机字节(16)。
%%转换为十六进制
列表:展平(io_lib:format(“~32.16.0b”,[X]))。

要获得相同的结果,在Elixir中,您可以使用Erlang模块
:crypto.strong\u rand\u bytes(16)
生成随机数,并使用
Base.encode16对其进行转换

查看以更好地理解
Base
模块 . 例如:


首先,您的ruby代码可以简化为:

SecureRandom.hex(16).upcase
其次,在erlang中,“字符串”可能是一个模糊的概念。在erlang中,通常更有效的方法是构造一个包含字符串块的列表,然后传递该列表,然后在需要输出字符串时,erlang将自动为您加入这些块:

-module(a).
-compile(export_all).

go() ->
    MyIolist = [ io_lib:format("~2.16.0B", [X]) || <<X>> <= crypto:strong_rand_bytes(16) ],

    %% Print a ruler  -------
    Ruler = "1234567890",
    lists:foreach(fun(_X) -> io:format("~s", [Ruler]) end,
                       lists:seq(1, 4) ),    
    io:format("~n"),
    %%-----------------------

    %% Print the list containing the chunks of the string:
    io:format("~s~n", [MyIolist]).
全文如下:

     2 => field width, 
    16 => base (base 10 is the default) 
     0 => padding character (so that the integers 0-15 are represented by two characters) 
    ~B => format code that allows you to specify the base of an integer and uses uppercase for letters
           (the particulars go between ~ and B)
在外壳中:

23> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}

24> a:go().
1234567890123456789012345678901234567890
21755436149C16E0A9902F7B7E9F6929
ok

25> a:go().
1234567890123456789012345678901234567890
86D3850FA2590B810C1CAB0B965EE415
ok

26> a:go().
1234567890123456789012345678901234567890
A9C31CAFCC74C8311A5389E3BA1CD19F
ok

27> a:go().
1234567890123456789012345678901234567890
FB82878326F7B6FDB87069AF031345FF
ok

28> a:go().
1234567890123456789012345678901234567890
57515F06DFA699710E543D9885CB90ED
ok

29> a:go().
1234567890123456789012345678901234567890
F84B308723DB660A8A4371E3A80C23DC
ok
42> c(a).  
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}

43> a:go().
1234567890123456789012345678901234567890
551589BE1CB7B0C048AEF67DD9343F7F
ok

44> a:go().
1234567890123456789012345678901234567890
AC3DC11F714D7F8FE8ABBED566E9AB5B
ok

45> a:go().
1234567890123456789012345678901234567890
176530F08AFEC3B5452A818E4A95C743
ok

46> a:go().
1234567890123456789012345678901234567890
8F2E651B3D7D53AF88147244BB8F6153
ok
我运行了几次,以便您可以看到字符串都是相同的长度。16字节*2个十六进制字符/字节=32个字符

正如Venkatakumar Srinivasan所演示的,出于您的目的,您可以将所有16个字节(128位)视为一个整数:

-module(a).
-compile(export_all).

go() ->
    <<X:128>> = crypto:strong_rand_bytes(16),
    Result = io_lib:format("~32.16.0B", [X]),

    %% Print a ruler:
    Ruler = "1234567890",
    lists:foreach(fun(_X) -> io:format("~s", [Ruler]) end,
                       lists:seq(1, 4) ),    
    io:format("~n"),
    %%-----------------------

    %% Print the list containing the chunks of the string:
    io:format("~s~n", Result).
没有理由调用
flant()
。虽然,
io_-lib:format()
本身并不关心创建完整的字符串,但当您告诉erlang使用格式序列
~s
输出由
io_-lib:format()
返回的列表时,erlang会将所有内容合并为一个字符串

-module(a).
-compile(export_all).

go() ->
    <<X:128>> = crypto:strong_rand_bytes(16),
    Result = io_lib:format("~32.16.0B", [X]),

    %% Print a ruler:
    Ruler = "1234567890",
    lists:foreach(fun(_X) -> io:format("~s", [Ruler]) end,
                       lists:seq(1, 4) ),    
    io:format("~n"),
    %%-----------------------

    %% Print the list containing the chunks of the string:
    io:format("~s~n", Result).
42> c(a).  
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}

43> a:go().
1234567890123456789012345678901234567890
551589BE1CB7B0C048AEF67DD9343F7F
ok

44> a:go().
1234567890123456789012345678901234567890
AC3DC11F714D7F8FE8ABBED566E9AB5B
ok

45> a:go().
1234567890123456789012345678901234567890
176530F08AFEC3B5452A818E4A95C743
ok

46> a:go().
1234567890123456789012345678901234567890
8F2E651B3D7D53AF88147244BB8F6153
ok