{"record":{"id":"b570750f164c092a","repo":"MuntashirAkon/AppManager","slug":"invalid-hex-char-s","errorCode":null,"errorMessage":"Invalid hex char '%s'","messagePattern":"Invalid hex char '(.+?)'","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"libcore/compat/src/main/java/io/github/muntashirakon/compat/HexDump.java","lineNumber":36,"sourceCode":"        char[] digits = upperCase ? HEX_DIGITS : HEX_LOWER_CASE_DIGITS;\n        char[] buf = new char[length * 2];\n\n        int bufIndex = 0;\n        for (int i = offset; i < offset + length; i++) {\n            byte b = array[i];\n            buf[bufIndex++] = digits[(b >>> 4) & 0x0F];\n            buf[bufIndex++] = digits[b & 0x0F];\n        }\n\n        return new String(buf);\n    }\n\n    private static int toByte(char c) {\n        if (c >= '0' && c <= '9') return (c - '0');\n        if (c >= 'A' && c <= 'F') return (c - 'A' + 10);\n        if (c >= 'a' && c <= 'f') return (c - 'a' + 10);\n\n        throw new RuntimeException(\"Invalid hex char '\" + c + \"'\");\n    }\n\n    public static byte[] hexStringToByteArray(String hexString) {\n        int length = hexString.length();\n        byte[] buffer = new byte[length / 2];\n\n        for (int i = 0; i < length; i += 2) {\n            buffer[i / 2] = (byte) ((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i + 1)));\n        }\n\n        return buffer;\n    }\n}\n","sourceCodeStart":18,"sourceCodeEnd":50,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/compat/src/main/java/io/github/muntashirakon/compat/HexDump.java#L18-L50","documentation":"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.","triggerScenarios":"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.","commonSituations":"Pasting hex dumps that include '0x' prefixes, colons, or spaces; user-supplied hashes/keys with typos; decoding output from a formatter that prepends offsets.","solutions":["Strip non-hex characters (prefixes like 0x, whitespace, separators) before conversion","Validate the string with a regex ^[0-9a-fA-F]+$ before calling","Catch the RuntimeException and surface a user-friendly 'not valid hex' message"],"exampleFix":"// before\nbyte[] b = HexDump.hexStringToByteArray(userInput); // may throw\n// after\nString clean = userInput.replaceAll(\"^(0x)?\\\\s+|[ :\\\\-]\", \"\");\nif (!clean.matches(\"[0-9a-fA-F]+\")) throw new IllegalArgumentException(\"not valid hex\");\nbyte[] b = HexDump.hexStringToByteArray(clean);","handlingStrategy":"validation","validationCode":"if (!hex.matches(\"[0-9a-fA-F]+\")) throw new IllegalArgumentException(\"invalid hex string\");","typeGuard":"boolean isHexString(String s) { return s != null && s.length() % 2 == 0 && s.matches(\"[0-9a-fA-F]+\"); }","tryCatchPattern":"try { byte[] b = HexDump.hexStringToByteArray(s); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Invalid hex char\")) { /* reject or sanitize input */ } else throw e; }","preventionTips":["Normalize input: strip 0x prefixes, whitespace, and separators","Validate with a hex regex before conversion","Ensure even length as well as valid characters"],"tags":["hex","parsing","validation"],"backgroundTag":"invalid-argument-format","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}