aboutsummaryrefslogtreecommitdiff
path: root/src/jlib.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/jlib.erl')
-rw-r--r--src/jlib.erl18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/jlib.erl b/src/jlib.erl
index 580ad1ffa..6913542f9 100644
--- a/src/jlib.erl
+++ b/src/jlib.erl
@@ -37,6 +37,7 @@
-export([tolower/1, term_to_base64/1, base64_to_term/1,
decode_base64/1, encode_base64/1, ip_to_list/1,
+ hex_to_bin/1, hex_to_base64/1,
atom_to_binary/1, binary_to_atom/1, tuple_to_binary/1,
l2i/1, i2l/1, i2l/2, expr_to_term/1, term_to_expr/1,
queue_drop_while/2, queue_foldl/3, queue_foldr/3, queue_foreach/2]).
@@ -917,6 +918,23 @@ ip_to_list(undefined) ->
ip_to_list(IP) ->
list_to_binary(inet_parse:ntoa(IP)).
+-spec hex_to_bin(binary()) -> binary().
+
+hex_to_bin(Hex) ->
+ hex_to_bin(binary_to_list(Hex), []).
+
+-spec hex_to_bin(list(), list()) -> binary().
+
+hex_to_bin([], Acc) ->
+ list_to_binary(lists:reverse(Acc));
+hex_to_bin([H1, H2 | T], Acc) ->
+ {ok, [V], []} = io_lib:fread("~16u", [H1, H2]),
+ hex_to_bin(T, [V | Acc]).
+
+-spec hex_to_base64(binary()) -> binary().
+
+hex_to_base64(Hex) -> encode_base64(hex_to_bin(Hex)).
+
binary_to_atom(Bin) ->
erlang:binary_to_atom(Bin, utf8).