{"record":{"id":"d542c8a20d3d8269","repo":"gchq/CyberChef","slug":"value-utils-bin-n-4-is-not-in-the-encoding-sc","errorCode":null,"errorMessage":"Value ${Utils.bin(n, 4)} is not in the encoding scheme","messagePattern":"Value (.+?) is not in the encoding scheme","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/FromBCD.mjs","lineNumber":114,"sourceCode":"            // Discard each high nibble\n            for (let i = 0; i < nibbles.length; i++) {\n                nibbles.splice(i, 1); // lgtm [js/loop-iteration-skipped-due-to-shifting]\n            }\n        }\n\n        if (signed) {\n            const sign = nibbles.pop();\n            if (sign === 13 ||\n                sign === 11) {\n                // Negative\n                output += \"-\";\n            }\n        }\n\n        nibbles.forEach(n => {\n            if (isNaN(n)) throw new OperationError(\"Invalid input\");\n            const val = encoding.indexOf(n);\n            if (val < 0) throw new OperationError(`Value ${Utils.bin(n, 4)} is not in the encoding scheme`);\n            output += val.toString();\n        });\n\n        return new BigNumber(output);\n    }\n\n}\n\nexport default FromBCD;\n","sourceCodeStart":96,"sourceCodeEnd":124,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/FromBCD.mjs#L96-L124","documentation":"Thrown by From BCD when a parsed nibble value is not present in the selected encoding scheme's lookup array (encoding.indexOf(n) < 0). Each BCD scheme (8 4 2 1, Excess-3, etc.) only accepts certain 4-bit patterns; a nibble outside that set is invalid for the scheme. The offending 4-bit value is shown in binary for diagnostics.","triggerScenarios":"Selecting the wrong BCD scheme for the data (e.g. feeding Excess-3 data as 8 4 2 1); data containing nibble values like 0xA-0xF which are invalid in plain BCD; unsigned data interpreted under a scheme that excludes the encountered patterns.","commonSituations":"Not knowing which BCD variant the source used; legacy mainframe/EBCDIC data with a different scheme; mixing packed/unpacked assumptions.","solutions":["Identify the correct BCD encoding scheme used by the source system and select it.","If the data may contain hex digits, verify it is actually BCD and not plain hex.","Try alternate schemes (8 4 2 1, 4 2 2 1, 5 4 2 1, Excess-3) to find the matching one."],"exampleFix":"// before: wrong scheme for the data\nfromBcd.run(input, ['8 4 2 1', true, false, 'Raw']) // nibble 0xA not in 8421\n// after: use a scheme that accepts the pattern, or confirm data is BCD\nfromBcd.run(input, ['Excess-3', true, false, 'Raw'])","handlingStrategy":"validation","validationCode":"// Check each nibble's 4-bit value is valid for the chosen scheme before decoding\nconst validSet = ENCODING_LOOKUP[scheme];\n// after splitting into nibbles, ensure encoding.includes(n) for each\nif (!nibbles.every(n => validSet.includes(n))) {\n  // pick a different scheme or reject the data\n}","typeGuard":"function nibbleInScheme(n, scheme) {\n  return ENCODING_LOOKUP[scheme].includes(n);\n}","tryCatchPattern":"try {\n  fromBcd.run(input, args);\n} catch (e) {\n  if (e.type === 'OperationError' && /is not in the encoding scheme/.test(e.message)) {\n    // wrong scheme; try alternate schemes (8421, 4221, 5421, Excess-3)\n  } else throw e;\n}","preventionTips":["Identify the source system's BCD variant before selecting the scheme.","If unsure, try the common schemes in turn to find the matching one.","Reject nibbles 0xA-0xF for plain 8 4 2 1 BCD."],"tags":["bcd","encoding","scheme","input-validation","parsing"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}