Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created April 4, 2024 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecakes/85aacf69b112c2e911e5d102dc51945b to your computer and use it in GitHub Desktop.
Save codecakes/85aacf69b112c2e911e5d102dc51945b to your computer and use it in GitHub Desktop.
Convert to Hex and back
defmodule ToHex do
@hex_base 16
def to_hex(0), do: 0
def to_hex(num) when is_number(num) do
div(num, @hex_base)
|> IO.inspect
|> then(& {to_hex(&1), rem(num, @hex_base)})
end
def reduce({a, b}, acc) when is_tuple(a) and is_number(b), do: reduce(a, [b | acc])
def reduce({a, b}, acc) when is_number(a) and is_number(b), do: [a | [b | acc]]
def to_decimal(<<digits::binary>> = hex_string), do: Integer.parse(digits, @hex_base) |> elem(0)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment