Created
February 21, 2013 09:10
-
-
Save Gustav-Simonsson/5003397 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
%%%============================================================================ | |
-spec bin_to_hexdigest(binary()) -> binary(). | |
bin_to_hexdigest(Bin) when is_binary(Bin) -> | |
<<<<(binary:list_to_bin( | |
case length(S = integer_to_list(I, 16)) of 1 -> [48|S]; 2 -> S end | |
))/bytes>> || <<I>> <= Bin>>. | |
-spec hexdigest_to_bin(binary()) -> binary(). | |
hexdigest_to_bin(BinHex) when is_binary(BinHex) -> | |
<<<<(list_to_integer([I1,I2], 16))>> || <<I1,I2>> <= BinHex>>. | |
%% @doc Convert spaced hexdigest to binary. Used for testing and debugging. | |
-spec spacehex_to_bin(binary() | list()) -> binary(). | |
spacehex_to_bin(Bin) when is_binary(Bin) -> | |
spacehex_to_bin(binary_to_list(Bin)); | |
spacehex_to_bin(List) -> | |
binary:list_to_bin( | |
[erlang:list_to_integer(S, 16) || S <- string:tokens(List, " ")]). | |
%% @doc Convert binary to spaced hexdigest representation. | |
-spec bin_to_spacehex(binary()) -> binary(). | |
bin_to_spacehex(Bin) -> | |
bin_to_spacehex(bin_to_hexdigest(Bin), <<>>). | |
bin_to_spacehex(<<>>, R) -> | |
Size = size(R), | |
if | |
Size > 0 -> binary:part(R, {0, Size-1}); | |
true -> R | |
end; | |
bin_to_spacehex(<<Hex:2/binary, Left/binary>>, Acc) -> | |
bin_to_spacehex(Left, <<Acc/binary, Hex/binary, " ">>). | |
%%%============================================================================ | |
%%% Tests | |
%%%============================================================================ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment