MuntashirAkon/AppManager · error · RuntimeException

Invalid hex char

Error message

Invalid hex char '%s'

What it means

HexDump.toByte converts a single hex character ('0'-'9','A'-'F','a'-'f') to its numeric value and throws an untyped RuntimeException for any other character, meaning the input string contained a non-hex character.

Solutions

  1. Strip non-hex characters (prefixes like 0x, whitespace, separators) before conversion
  2. Validate the string with a regex ^[0-9a-fA-F]+$ before calling
  3. Catch the RuntimeException and surface a user-friendly 'not valid hex' message

Example fix

// before
byte[] b = HexDump.hexStringToByteArray(userInput); // may throw
// after
String clean = userInput.replaceAll("^(0x)?\\s+|[ :\\-]", "");
if (!clean.matches("[0-9a-fA-F]+")) throw new IllegalArgumentException("not valid hex");
byte[] b = HexDump.hexStringToByteArray(clean);
Defensive patterns

Strategy: validation

Validate before calling

if (!hex.matches("[0-9a-fA-F]+")) throw new IllegalArgumentException("invalid hex string");

Type guard

boolean isHexString(String s) { return s != null && s.length() % 2 == 0 && s.matches("[0-9a-fA-F]+"); }

Try / catch

try { byte[] b = HexDump.hexStringToByteArray(s); } catch (RuntimeException e) { if (e.getMessage().startsWith("Invalid hex char")) { /* reject or sanitize input */ } else throw e; }

Prevention

When it happens

Trigger: Calling hexStringToByteArray with a string containing characters outside 0-9/A-F/a-f — e.g. an odd '0x' prefix, whitespace, '-', 'g'-'z', or other non-hex separators.

Common situations: Pasting hex dumps that include '0x' prefixes, colons, or spaces; user-supplied hashes/keys with typos; decoding output from a formatter that prepends offsets.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/b570750f164c092a. Report an issue: GitHub.

Appendix: source

Thrown at libcore/compat/src/main/java/io/github/muntashirakon/compat/HexDump.java:36

        char[] digits = upperCase ? HEX_DIGITS : HEX_LOWER_CASE_DIGITS;
        char[] buf = new char[length * 2];

        int bufIndex = 0;
        for (int i = offset; i < offset + length; i++) {
            byte b = array[i];
            buf[bufIndex++] = digits[(b >>> 4) & 0x0F];
            buf[bufIndex++] = digits[b & 0x0F];
        }

        return new String(buf);
    }

    private static int toByte(char c) {
        if (c >= '0' && c <= '9') return (c - '0');
        if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
        if (c >= 'a' && c <= 'f') return (c - 'a' + 10);

        throw new RuntimeException("Invalid hex char '" + c + "'");
    }

    public static byte[] hexStringToByteArray(String hexString) {
        int length = hexString.length();
        byte[] buffer = new byte[length / 2];

        for (int i = 0; i < length; i += 2) {
            buffer[i / 2] = (byte) ((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i + 1)));
        }

        return buffer;
    }
}

View on GitHub (pinned to 0152f468fc)