Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: ddb90419d5
Fetching contributors…

Cannot retrieve contributors at this time

24 lines (19 sloc) 0.602 kB
%
% via Steve Vinoski
% http://necrobious.blogspot.com/2008/03/binary-to-hex-string-back-to-binary-in.html
%
-module(hex).
-export([bin_to_hexstr/1, list_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)]).
list_to_hexstr(Bin) ->
lists:flatten([io_lib:format("~2.16.0b", [X]) ||
X <- 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]).
Jump to Line
Something went wrong with that request. Please try again.