{"record":{"id":"0359489f6b8321d9","repo":"apache/cassandra","slug":"a-cql-blob-string-must-start-with-0x","errorCode":null,"errorMessage":"A CQL blob string must start with \"0x\"","messagePattern":"A CQL blob string must start with \"0x\"","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/utils/Bytes.java","lineNumber":149,"sourceCode":"    /**\n     * Parse an hex string representing a CQL blob.\n     *\n     * <p>The input should be a valid representation of a CQL blob, i.e. it must start by \"0x\"\n     * followed by the hexadecimal representation of the blob bytes.\n     *\n     * @param str the CQL blob string representation to parse.\n     * @return the bytes corresponding to {@code str}. If {@code str} is {@code null}, this method\n     * returns {@code null}.\n     * @throws IllegalArgumentException if {@code str} is not a valid CQL blob string.\n     */\n    public static ByteBuffer fromHexString(String str)\n    {\n        if ((str.length() & 1) == 1)\n            throw new IllegalArgumentException(\n            \"A CQL blob string must have an even length (since one byte is always 2 hexadecimal character)\");\n\n        if (str.charAt(0) != '0' || str.charAt(1) != 'x')\n            throw new IllegalArgumentException(\"A CQL blob string must start with \\\"0x\\\"\");\n\n        return ByteBuffer.wrap(fromRawHexString(str, 2));\n    }\n\n    /**\n     * Extract the content of the provided {@code ByteBuffer} as a byte array.\n     *\n     * <p>This method work with any type of {@code ByteBuffer} (direct and non-direct ones), but when\n     * the {@code ByteBuffer} is backed by an array, this method will try to avoid copy when possible.\n     * As a consequence, changes to the returned byte array may or may not reflect into the initial\n     * {@code ByteBuffer}.\n     *\n     * @param bytes the buffer whose content to extract.\n     * @return a byte array with the content of {@code bytes}. That array may be the array backing\n     * {@code bytes} if this can avoid a copy.\n     */\n    public static byte[] getArray(ByteBuffer bytes)\n    {","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/utils/Bytes.java#L131-L167","documentation":"Bytes.fromHexString requires the CQL blob literal form, which must start with the two characters '0x'. A string with any other prefix (or empty) throws this IllegalArgumentException. Because the even-length check precedes it, a valid-length string without '0x' reports this message.","triggerScenarios":"fromHexString(\"abcdef\"), passing a Base64 string or a plain byte-array hex dump without the CQL prefix, passing output of Integer.toHexString (no 0x).","commonSituations":"Interoperating with tools that emit bare hex (some log formatters) instead of CQL literals; converting from other languages' hex encodings; users pasting raw hex into configuration.","solutions":["Prepend \"0x\" to the string before calling fromHexString.","Use Bytes.fromRawHexString if you have bare hex without the prefix and that fits your flow.","Normalize input: case does not matter, but the 0x prefix is mandatory.","Centralize blob-string formatting through Bytes.toHexString so outputs are always 0x-prefixed."],"exampleFix":"// before\nByteBuffer bb = Bytes.fromHexString(\"abcdef\");\n// after\nByteBuffer bb = Bytes.fromHexString(\"0xabcdef\");","handlingStrategy":"validation","validationCode":"if (!str.startsWith(\"0x\")) throw new IllegalArgumentException(\"blob literal must start with 0x: \" + str); // or normalize: str = \"0x\" + str","typeGuard":null,"tryCatchPattern":"try { return Bytes.fromHexString(s); } catch (IllegalArgumentException e) { if (!s.startsWith(\"0x\")) return Bytes.fromHexString(\"0x\" + s); throw e; }","preventionTips":["Only produce blob strings with Bytes.toHexString","Accept both raw hex and CQL form at API boundaries and normalize","Document the 0x requirement wherever blob strings are input"],"tags":["blob","hex","format"],"backgroundTag":"invalid-argument-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}