summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordu.net>2014-04-26 08:06:56 +0200
committerLinus Nordberg <linus@nordu.net>2014-04-26 08:06:56 +0200
commitb15cbb8402a0e8f8b5a6b1e7e48c9c1a7036742b (patch)
tree3e930c0a90926675aa00ad7105eb16404b50fb96 /src
parent59e9f6b3af3d421834cd1c5411ea0eae3a83a7dd (diff)
Add hex.erl.
Diffstat (limited to 'src')
-rw-r--r--src/hex.erl14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/hex.erl b/src/hex.erl
new file mode 100644
index 0000000..dcb9349
--- /dev/null
+++ b/src/hex.erl
@@ -0,0 +1,14 @@
+-module(hex).
+-export([bin_to_hexstr/1,hexstr_to_bin/1]).
+
+bin_to_hexstr(Bin) ->
+ lists:flatten([io_lib:format("~2.16.0B", [X]) ||
+ X <- binary_to_list(Bin)]).
+
+hexstr_to_bin(S) ->
+ hexstr_to_bin(S, []).
+hexstr_to_bin([], Acc) ->
+ list_to_binary(lists:reverse(Acc));
+hexstr_to_bin([X,Y|T], Acc) ->
+ {ok, [V], []} = io_lib:fread("~16u", [X,Y]),
+ hexstr_to_bin(T, [V | Acc]).