Created
May 6, 2009 21:09
-
-
Save skeptomai/107744 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% @author <cb@opscode.com> | |
%% @doc Simple hex to binary and back, taken from comments by Steve Vinoski and others | |
-module(hex). | |
-author('<cb@opscode.com>'). | |
-export([bin_to_hexstr/1,hexstr_to_bin/1]). | |
%% External API | |
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]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment