{"record":{"id":"8701f4b03121d74e","repo":"apache/dubbo","slug":"hex-string-format-error-c","errorCode":null,"errorMessage":"hex string format error [${c}].","messagePattern":"hex string format error \\[(.+?)\\]\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java","lineNumber":876,"sourceCode":"     *\n     * @param is input stream.\n     * @return MD5 byte array.\n     */\n    public static byte[] getMD5(InputStream is) throws IOException {\n        return getMD5(is, 1024 * 8);\n    }\n\n    private static byte hex(char c) {\n        if (c <= '9') {\n            return (byte) (c - '0');\n        }\n        if (c >= 'a' && c <= 'f') {\n            return (byte) (c - 'a' + 10);\n        }\n        if (c >= 'A' && c <= 'F') {\n            return (byte) (c - 'A' + 10);\n        }\n        throw new IllegalArgumentException(\"hex string format error [\" + c + \"].\");\n    }\n\n    private static int indexOf(char[] cs, char c) {\n        for (int i = 0, len = cs.length; i < len; i++) {\n            if (cs[i] == c) {\n                return i;\n            }\n        }\n        return -1;\n    }\n\n    private static byte[] decodeTable(String code) {\n        int hash = code.hashCode();\n        byte[] ret = DECODE_TABLE_MAP.get(hash);\n        if (ret == null) {\n            if (code.length() < 64) {\n                throw new IllegalArgumentException(\"Base64 code length < 64.\");\n            }","sourceCodeStart":858,"sourceCodeEnd":894,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java#L858-L894","documentation":"Thrown by the private hex(char) helper (Bytes.java:866) when a character is not a valid hex digit (outside '0'-'9', 'a'-'f', 'A'-'F'). It is reachable through the public Bytes.hex2bytes(String) and hex2bytes(String, int, int) methods: after those validate parity and bounds, they iterate the string and call hex() per character, so any non-hex char surfaces as IllegalArgumentException with the offending character in the message.","triggerScenarios":"Calling Bytes.hex2bytes(\"48656c6c6f\") is fine; calling hex2bytes(\"48656c6c6g\") (note 'g') or hex2bytes(\"de/ad\") (contains '/') triggers it; also hex2bytes on input with whitespace, '0x' prefix, or wrong case region.","commonSituations":"Hex strings copied with a '0x' prefix or spaces/colons/dashes between bytes; uppercase/lowercase assumed but a non-hex char slipped in; a digest/UUID string with separators passed straight through; truncated nibble.","solutions":["Strip separators and prefixes before decoding: str = str.replaceAll(\"[^0-9A-Fa-f]\", \"\").","Remove a leading '0x' if present: if (str.startsWith(\"0x\")) str = str.substring(2);","Validate with a regex first so you control the error message: if (!str.matches(\"[0-9A-Fa-f]*\")) ... (note hex2bytes also requires even length)."],"exampleFix":"// before\nbyte[] b = Bytes.hex2bytes(\"de:ad:be:ef\"); // ':' is not a hex digit\n// after\nString clean = \"de:ad:be:ef\".replace(\":\", \"\");\nbyte[] b = Bytes.hex2bytes(clean);","handlingStrategy":"validation","validationCode":"String clean = str.replaceAll(\"[^0-9A-Fa-f]\", \"\");\nif (clean.length() % 2 != 0) throw new IllegalArgumentException(\"hex length must be even\");\nbyte[] b = Bytes.hex2bytes(clean);","typeGuard":"static boolean isHexString(String s) {\n    return s != null && s.matches(\"[0-9A-Fa-f]*\") && s.length() % 2 == 0;\n}","tryCatchPattern":"try {\n    byte[] b = Bytes.hex2bytes(str);\n} catch (IllegalArgumentException e) {\n    // non-hex char or odd length — reject the untrusted input\n    throw new IllegalArgumentException(\"invalid hex input\", e);\n}","preventionTips":["Strip separators (':', '-', ' ') and any '0x' prefix before hex2bytes.","Validate hex with a regex before decoding to control the error.","Ensure even character count — each byte is two hex digits."],"tags":["hex","encoding","decoding","format","validation","dubbo-common"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}