{"record":{"id":"84f8e1cb009ac1fe","repo":"zxing/zxing","slug":"illegal-character-encountered-stringrepresentati","errorCode":null,"errorMessage":"illegal character encountered: {stringRepresentation.substring(pos)}","messagePattern":"illegal character encountered: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/google/zxing/common/BitMatrix.java","lineNumber":129,"sourceCode":"          if (rowLength == -1) {\n            rowLength = bitsPos - rowStartPos;\n          } else if (bitsPos - rowStartPos != rowLength) {\n            throw new IllegalArgumentException(\"row lengths do not match\");\n          }\n          rowStartPos = bitsPos;\n          nRows++;\n        }\n        pos++;\n      }  else if (stringRepresentation.startsWith(setString, pos)) {\n        pos += setString.length();\n        bits[bitsPos] = true;\n        bitsPos++;\n      } else if (stringRepresentation.startsWith(unsetString, pos)) {\n        pos += unsetString.length();\n        bits[bitsPos] = false;\n        bitsPos++;\n      } else {\n        throw new IllegalArgumentException(\n            \"illegal character encountered: \" + stringRepresentation.substring(pos));\n      }\n    }\n\n    // no EOL at end?\n    if (bitsPos > rowStartPos) {\n      if (rowLength == -1) {\n        rowLength = bitsPos - rowStartPos;\n      } else if (bitsPos - rowStartPos != rowLength) {\n        throw new IllegalArgumentException(\"row lengths do not match\");\n      }\n      nRows++;\n    }\n\n    BitMatrix matrix = new BitMatrix(rowLength, nRows);\n    for (int i = 0; i < bitsPos; i++) {\n      if (bits[i]) {\n        matrix.set(i % rowLength, i / rowLength);","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/zxing/zxing/blob/19aa2d8254410e161f04dc3c928e68d5e90233c2/core/src/main/java/com/google/zxing/common/BitMatrix.java#L111-L147","documentation":"Thrown by BitMatrix.parse(String, setString, unsetString) when the parser encounters a substring at the current position that matches neither setString nor unsetString, and is not a newline/carriage-return. The exception message includes the unparsed remainder of the string from the failure point.","triggerScenarios":"Calling BitMatrix.parse() where setString and unsetString tokens don't cover all characters in the input — e.g., passing setString=\"##\" and unsetString=\"  \" (two spaces) but the input contains single characters, or the input has stray whitespace, tabs, or punctuation.","commonSituations":"Mismatch between the token width used in the input string and the setString/unsetString arguments (e.g., single-char input with multi-char tokens or vice versa); non-ASCII whitespace characters in the input; trailing spaces or BOM characters.","solutions":["Ensure every non-newline character in stringRepresentation is covered by either setString or unsetString — verify token lengths match","Pre-sanitize the input: remove tabs, trim BOM, normalize line endings to '\\n'","If the input uses single characters '1'/'0' or '#'/' ', pass single-character setString/unsetString"],"exampleFix":"// before\nBitMatrix.parse(\"## ## ##\\n## ##  \", \"## \", \"   \");\n// token width mismatch causes illegal character\n\n// after\nBitMatrix.parse(\"10110\\n01101\", \"1\", \"0\");\n// single-char tokens, clean newlines","handlingStrategy":"validation","validationCode":"static boolean isParseable(String repr, String setStr, String unsetStr) {\n    if (repr == null) return false;\n    int pos = 0;\n    while (pos < repr.length()) {\n        char c = repr.charAt(pos);\n        if (c == '\\n' || c == '\\r') { pos++; continue; }\n        if (repr.startsWith(setStr, pos)) { pos += setStr.length(); }\n        else if (repr.startsWith(unsetStr, pos)) { pos += unsetStr.length(); }\n        else return false;\n    }\n    return true;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure the set/unset token length matches the spacing in the input string","Pre-sanitize: strip tabs, BOM, non-breaking spaces, and other invisible characters","Use toString() output from a known-valid matrix as the reference format for parse()"],"tags":["bitmatrix","parsing","validation","zxing"],"backgroundTag":null,"analyzedSha":"19aa2d8254410e161f04dc3c928e68d5e90233c2","analyzedAt":"2026-08-14T03:07:53.428Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}