Created
April 4, 2024 15:50
-
-
Save codecakes/85aacf69b112c2e911e5d102dc51945b to your computer and use it in GitHub Desktop.
Convert to Hex and back
This file contains hidden or 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
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