{"record":{"id":"bc050e56d33c8334","repo":"gchq/CyberChef","slug":"unexpected-character-encountered-character","errorCode":null,"errorMessage":"unexpected character encountered: \"${character}\"","messagePattern":"unexpected character encountered: \"(.+?)\"","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"warning","filePath":"src/core/lib/ParityBit.mjs","lineNumber":24,"sourceCode":" * @license Apache-2.0\n *\n */\n\nimport OperationError from \"../errors/OperationError.mjs\";\n\n/**\n * Function to take the user input and encode using the given arguments\n * @param input string of binary\n * @param args array\n */\nexport function calculateParityBit(input, args) {\n    let count1s = 0;\n    for (let i = 0; i < input.length; i++) {\n        const character = input.charAt(i);\n        if (character === \"1\") {\n            count1s++;\n        } else if (character !== args[3] && character !== \"0\" && character !== \" \") {\n            throw new OperationError(\"unexpected character encountered: \\\"\" + character + \"\\\"\");\n        }\n    }\n    let parityBit = \"1\";\n    const flipflop = args[0] === \"Even Parity\" ? 0 : 1;\n    if (count1s % 2 === flipflop) {\n        parityBit = \"0\";\n    }\n    if (args[1] === \"End\") {\n        return input + parityBit;\n    } else {\n        return parityBit + input;\n    }\n}\n\n/**\n * just removes the parity bit to return the original data\n * @param input string of binary, encoded\n * @param args array","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/ParityBit.mjs#L6-L42","documentation":"calculateParityBit iterates the input string expecting only '1', '0', ' ' (space), or the parity-bit placeholder character supplied in args[3]. Any other character means the input is not a valid binary-with-placeholder string, so it is rejected with the offending character echoed back.","triggerScenarios":"Calling calculateParityBit(input, args) where input contains letters, symbols, newlines, tabs, or any character that is not '0', '1', ' ', or args[3]. args[3] is the configured parity-bit placeholder (often 'x' or 'p').","commonSituations":"Feeding raw text instead of a binary string; the input was meant to be converted from bytes to binary first; the parity placeholder character in args[3] does not match the placeholder actually used in the data; copy-paste introduced non-breaking spaces or newlines; mismatched argument array shape (args[3] undefined).","solutions":["Convert the input to a binary string first (e.g. via CyberChef's 'To Binary' operation).","Ensure args[3] matches the placeholder character used in your data, or remove placeholders before calling.","Strip whitespace and newlines: input.replace(/[\\r\\n]/g, '').","If args is shaped differently in your integration, normalize it so args[3] holds the placeholder string."],"exampleFix":"// before\nconst out = ParityBit.calculateParityBit('A1101', ['Even Parity','End','','x']); // 'A' rejected\n\n// after\nconst binary = '1101'; // pure binary, no stray chars\nconst out = ParityBit.calculateParityBit(binary, ['Even Parity','End','','x']);\n// or strip unwanted chars first:\nconst clean = raw.replace(/[^01 x]/g, '');","handlingStrategy":"validation","validationCode":"function sanitizeParityInput(input, placeholder) {\n  const allow = new Set(['0','1',' ',placeholder]);\n  return [...input].filter(c => allow.has(c)).join('');\n}\n\nconst clean = sanitizeParityInput(rawBinary, args[3]);\nreturn ParityBit.calculateParityBit(clean, args);","typeGuard":null,"tryCatchPattern":"try {\n  return ParityBit.calculateParityBit(input, args);\n} catch (e) {\n  if (e instanceof OperationError && /unexpected character/.test(e.message)) {\n    throw new Error('Input must be binary (0/1), spaces, or the placeholder ' + args[3]);\n  }\n  throw e;\n}","preventionTips":["Convert bytes/text to a binary string before calling calculateParityBit.","Make sure args[3] matches the placeholder character used in the input.","Strip newlines, tabs, and non-breaking spaces from input.","Validate that args has at least 4 elements so args[3] is defined."],"tags":["encoding","parity","binary","validation","user-input"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}