Erlang io:formatting a binary to hex

后端 未结 7 696
难免孤独
难免孤独 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:08

    Here is another short and fast version which I use:

    hexlify(Bin) when is_binary(Bin) ->
        << <<(hex(H)),(hex(L))>> || <> <= Bin >>.
    
    hex(C) when C < 10 -> $0 + C;
    hex(C) -> $a + C - 10.
    

提交回复
热议问题