-module(otpbp_binary). -ifndef(HAVE_binary__decode_hex_1). % OTP 24.0 -export([decode_hex/1]). -endif. -ifndef(HAVE_binary__encode_hex_1). % OTP 24.0 -export([encode_hex/1]). -endif. -ifndef(HAVE_binary__encode_hex_2). % OTP 25.3 -export([encode_hex/2]). -endif. -ifndef(HAVE_binary__decode_hex_1). decode_hex(Bin) when byte_size(Bin) rem 2 =:= 0 -> <<<<(decode_hex_char(A)):4, (decode_hex_char(B)):4>> || <> <= Bin>>; decode_hex(Bin) -> error(badarg, [Bin]). decode_hex_char(Char) when Char >= $a, Char =< $f -> Char - ($a - 10); decode_hex_char(Char) when Char >= $A, Char =< $F -> Char - ($A - 10); decode_hex_char(Char) when Char >= $0, Char =< $9 -> Char - $0; decode_hex_char(Char) -> error(badarg, [Char]). -endif. -ifndef(HAVE_binary__encode_hex_1). encode_hex(Bin) when is_binary(Bin) -> <<<<(encode_hex_digit(A)), (encode_hex_digit(B))>> || <> <= Bin>>; encode_hex(Bin) -> error(badarg, [Bin]). encode_hex_digit(Char) when Char =< 9 -> Char + $0; encode_hex_digit(Char) -> Char + ($A - 10). -endif. -ifndef(HAVE_binary__encode_hex_2). -ifdef(HAVE_binary__encode_hex_1). encode_hex(Bin, uppercase) when is_binary(Bin) -> binary:encode_hex(Bin); encode_hex(Bin, lowercase) when is_binary(Bin) -> <<<<(encode_hex_digit_lowercase(A)), (encode_hex_digit_lowercase(B))>> || <> <= Bin>>; encode_hex(Bin, Case) -> error(badarg, [Bin, Case]). encode_hex_digit_lowercase(Char) when Char =< 9 -> Char + $0; encode_hex_digit_lowercase(Char) -> Char + ($a - 10). -else. encode_hex(Bin, Case) when is_binary(Bin), Case =:= uppercase orelse Case =:= lowercase -> <<<<(encode_hex_digit(A, Case)), (encode_hex_digit(B, Case))>> || <> <= Bin>>; encode_hex(Bin, Case) -> error(badarg, [Bin, Case]). encode_hex_digit(Char, _) when Char =< 9 -> Char + $0; encode_hex_digit(Char, uppercase) -> Char + ($A - 10); encode_hex_digit(Char, _lowercase) -> Char + ($a - 10). -endif. -endif.