Erlang io:formatting a binary to hex

后端 未结 7 666
难免孤独
难免孤独 2020-12-29 06:06

Can I format an Erlang binary so that each byte is written in hex? I.e.,

> io:format(???, [<<255, 16>>]).
<>
相关标签:
7条回答
  • 2020-12-29 06:28

    if you prefer to make a binary string instead of erlang default list strings, you may use binary comprehension syntax, like what I did on my sha1 generating code:

    1> << << if N >= 10 -> N -10 + $a;
    1>          true    -> N     + $0 end >>
    1>    || <<N:4>> <= crypto:hash(sha, "hello world") >>.
    <<"2aae6c35c94fcfb415dbe95f408b9ce91ee846ed">>
    

    same as in python binascii.b2a_hex:

    >>> binascii.b2a_hex(sha.new('hello world').digest())
    '2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'
    
    0 讨论(0)
提交回复
热议问题