#!/usr/bin/env elixir Mix.install([]) defmodule QuickHexTest do def hex_to_binary(hex_string) when is_binary(hex_string) do try do # First validate that the string contains only valid hex characters if String.match?(hex_string, ~r/^[0-9A-Fa-f]+$/) do # Ensure even length (pad with leading zero if needed) padded_hex = if rem(String.length(hex_string), 2) == 1 do "0" <> hex_string else hex_string end case Base.decode16(padded_hex, case: :mixed) do {:ok, binary} -> {:ok, binary} :error -> {:error, :invalid_hex} end else {:error, :invalid_hex} end rescue _ -> {:error, :invalid_hex} end end def test do IO.puts("Testing hex validation:") # Valid hex result1 = hex_to_binary("6000782000") IO.puts("Valid hex '6000782000': #{inspect(result1)}") # Invalid hex result2 = hex_to_binary("invalid_hex") IO.puts("Invalid hex 'invalid_hex': #{inspect(result2)}") # Mixed valid/invalid result3 = hex_to_binary("60G0782000") IO.puts("Invalid hex '60G0782000': #{inspect(result3)}") # Empty string result4 = hex_to_binary("") IO.puts("Empty string '': #{inspect(result4)}") end end QuickHexTest.test()