Erlang io:formatting a binary to hex

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

    This hasn’t seen any action for a while, but all of the prior solutions seem overly convoluted. Here’s what, for me, seems much simpler:

    [begin if N < 10 -> 48 + N; true -> 87 + N end end || <> <= Bin]
    

    If you prefer it expanded a bit:

    [begin
        if
            N < 10 ->
                48 + N; % 48 = $0
            true ->
                87 + N  % 87 = ($a - 10)
        end
    end || <> <= Bin]
    

提交回复
热议问题