Skip to content
Newer
Older
100644 92 lines (71 sloc) 2.09 KB
4842019 @mdempsky first post
mdempsky authored
1 %% @author Bob Ippolito <bob@mochimedia.com>
2 %% @copyright 2006 Mochi Media, Inc.
3
4 %% @doc Utilities for working with hexadecimal strings.
5
6 -module(mochihex).
7 -author('bob@mochimedia.com').
8
ca962ce @etrepum refactor to use eunit, generate code coverage, update Makefiles and s…
etrepum authored
9 -export([to_hex/1, to_bin/1, to_int/1, dehex/1, hexdigit/1]).
4842019 @mdempsky first post
mdempsky authored
10
11 %% @type iolist() = [char() | binary() | iolist()]
12 %% @type iodata() = iolist() | binary()
13
14 %% @spec to_hex(integer | iolist()) -> string()
15 %% @doc Convert an iolist to a hexadecimal string.
16 to_hex(0) ->
17 "0";
18 to_hex(I) when is_integer(I), I > 0 ->
19 to_hex_int(I, []);
20 to_hex(B) ->
21 to_hex(iolist_to_binary(B), []).
22
23 %% @spec to_bin(string()) -> binary()
24 %% @doc Convert a hexadecimal string to a binary.
25 to_bin(L) ->
26 to_bin(L, []).
27
28 %% @spec to_int(string()) -> integer()
29 %% @doc Convert a hexadecimal string to an integer.
30 to_int(L) ->
31 erlang:list_to_integer(L, 16).
32
33 %% @spec dehex(char()) -> integer()
34 %% @doc Convert a hex digit to its integer value.
35 dehex(C) when C >= $0, C =< $9 ->
36 C - $0;
37 dehex(C) when C >= $a, C =< $f ->
38 C - $a + 10;
39 dehex(C) when C >= $A, C =< $F ->
40 C - $A + 10.
41
42 %% @spec hexdigit(integer()) -> char()
43 %% @doc Convert an integer less than 16 to a hex digit.
44 hexdigit(C) when C >= 0, C =< 9 ->
45 C + $0;
46 hexdigit(C) when C =< 15 ->
47 C + $a - 10.
48
49 %% Internal API
50
51 to_hex(<<>>, Acc) ->
52 lists:reverse(Acc);
53 to_hex(<<C1:4, C2:4, Rest/binary>>, Acc) ->
54 to_hex(Rest, [hexdigit(C2), hexdigit(C1) | Acc]).
55
56 to_hex_int(0, Acc) ->
57 Acc;
58 to_hex_int(I, Acc) ->
59 to_hex_int(I bsr 4, [hexdigit(I band 15) | Acc]).
60
61 to_bin([], Acc) ->
62 iolist_to_binary(lists:reverse(Acc));
63 to_bin([C1, C2 | Rest], Acc) ->
64 to_bin(Rest, [(dehex(C1) bsl 4) bor dehex(C2) | Acc]).
65
ca962ce @etrepum refactor to use eunit, generate code coverage, update Makefiles and s…
etrepum authored
66
67
68 %%
69 %% Tests
70 %%
71 -ifdef(TEST).
cb46038 @etrepum include eunit only if TEST is already defined
etrepum authored
72 -include_lib("eunit/include/eunit.hrl").
ca962ce @etrepum refactor to use eunit, generate code coverage, update Makefiles and s…
etrepum authored
73
74 to_hex_test() ->
75 "ff000ff1" = to_hex([255, 0, 15, 241]),
76 "ff000ff1" = to_hex(16#ff000ff1),
61e9420 @etrepum test coverage for mochihex
etrepum authored
77 "0" = to_hex(16#0),
ca962ce @etrepum refactor to use eunit, generate code coverage, update Makefiles and s…
etrepum authored
78 ok.
79
80 to_bin_test() ->
81 <<255, 0, 15, 241>> = to_bin("ff000ff1"),
61e9420 @etrepum test coverage for mochihex
etrepum authored
82 <<255, 0, 10, 161>> = to_bin("Ff000aA1"),
ca962ce @etrepum refactor to use eunit, generate code coverage, update Makefiles and s…
etrepum authored
83 ok.
84
85 to_int_test() ->
86 16#ff000ff1 = to_int("ff000ff1"),
61e9420 @etrepum test coverage for mochihex
etrepum authored
87 16#ff000aa1 = to_int("FF000Aa1"),
88 16#0 = to_int("0"),
ca962ce @etrepum refactor to use eunit, generate code coverage, update Makefiles and s…
etrepum authored
89 ok.
90
91 -endif.
Something went wrong with that request. Please try again.