gchq/CyberChef · warning · OperationError

unexpected character encountered: "${character}"

Error message

unexpected character encountered: "${character}"

What it means

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.

Source

Thrown at src/core/lib/ParityBit.mjs:24

 * @license Apache-2.0
 *
 */

import OperationError from "../errors/OperationError.mjs";

/**
 * Function to take the user input and encode using the given arguments
 * @param input string of binary
 * @param args array
 */
export function calculateParityBit(input, args) {
    let count1s = 0;
    for (let i = 0; i < input.length; i++) {
        const character = input.charAt(i);
        if (character === "1") {
            count1s++;
        } else if (character !== args[3] && character !== "0" && character !== " ") {
            throw new OperationError("unexpected character encountered: \"" + character + "\"");
        }
    }
    let parityBit = "1";
    const flipflop = args[0] === "Even Parity" ? 0 : 1;
    if (count1s % 2 === flipflop) {
        parityBit = "0";
    }
    if (args[1] === "End") {
        return input + parityBit;
    } else {
        return parityBit + input;
    }
}

/**
 * just removes the parity bit to return the original data
 * @param input string of binary, encoded
 * @param args array

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Convert the input to a binary string first (e.g. via CyberChef's 'To Binary' operation).
  2. Ensure args[3] matches the placeholder character used in your data, or remove placeholders before calling.
  3. Strip whitespace and newlines: input.replace(/[\r\n]/g, '').
  4. If args is shaped differently in your integration, normalize it so args[3] holds the placeholder string.

Example fix

// before
const out = ParityBit.calculateParityBit('A1101', ['Even Parity','End','','x']); // 'A' rejected

// after
const binary = '1101'; // pure binary, no stray chars
const out = ParityBit.calculateParityBit(binary, ['Even Parity','End','','x']);
// or strip unwanted chars first:
const clean = raw.replace(/[^01 x]/g, '');
Defensive patterns

Strategy: validation

Validate before calling

function sanitizeParityInput(input, placeholder) {
  const allow = new Set(['0','1',' ',placeholder]);
  return [...input].filter(c => allow.has(c)).join('');
}

const clean = sanitizeParityInput(rawBinary, args[3]);
return ParityBit.calculateParityBit(clean, args);

Try / catch

try {
  return ParityBit.calculateParityBit(input, args);
} catch (e) {
  if (e instanceof OperationError && /unexpected character/.test(e.message)) {
    throw new Error('Input must be binary (0/1), spaces, or the placeholder ' + args[3]);
  }
  throw e;
}

Prevention

When it happens

Trigger: 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').

Common situations: 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).

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/bc050e56d33c8334. Report an issue: GitHub.