2
\$\begingroup\$

Just mucking around with Elixir and bitstrings and wondering if there is a better way to implement the following ( or improve ). :-

   defmodule Crypto do
      def hexdigit( digit ) do
        case digit do
          d when d >= ?0 and d <= ?9    -> d - ?0
          c when c >= ?a and c <= ?f   -> c - ?a + 10
          c when c >= ?A and c <= ?F   -> c - ?A + 10
          _ -> 0
        end
      end

      def bin(hex) do
        Enum.map(to_char_list(hex), fn x -> <<hexdigit(x)::size(4)>> end) |> :erlang.list_to_bitstring
      end
    end

Note: I'm not looking for a call to a library to do the conversion to binary.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.