{"record":{"id":"2d1f3c57267e85fe","repo":"gchq/CyberChef","slug":"error-base-argument-must-be-between-2-and-36-2d1f3c","errorCode":null,"errorMessage":"Error: Base argument must be between 2 and 36","messagePattern":"Error: Base argument must be between 2 and 36","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ToCharcode.mjs","lineNumber":59,"sourceCode":"        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     *\n     * @throws {OperationError} if base argument out of range\n     */\n    run(input, args) {\n        const delim = Utils.charRep(args[0] || \"Space\"),\n            base = args[1];\n        let output = \"\",\n            padding,\n            ordinal;\n\n        if (base < 2 || base > 36) {\n            throw new OperationError(\"Error: Base argument must be between 2 and 36\");\n        }\n\n        const charcode = Utils.strToCharcode(input);\n        for (let i = 0; i < charcode.length; i++) {\n            ordinal = charcode[i];\n\n            if (base === 16) {\n                if (ordinal < 256) padding = 2;\n                else if (ordinal < 65536) padding = 4;\n                else if (ordinal < 16777216) padding = 6;\n                else if (ordinal < 4294967296) padding = 8;\n                else padding = 2;\n\n                if (padding > 2 && isWorkerEnvironment()) self.setOption(\"attemptHighlight\", false);\n\n                output += Utils.hex(ordinal, padding) + delim;\n            } else {\n                if (isWorkerEnvironment()) self.setOption(\"attemptHighlight\", false);","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ToCharcode.mjs#L41-L77","documentation":"To Charcode converts characters to their numeric code points in a chosen base, but JavaScript's number.toString(radix) only accepts radix 2-36. The guard rejects any base outside that inclusive range before the conversion loop.","triggerScenarios":"Setting the Base argument below 2 or above 36 (e.g. 1, 0, 64, or 100).","commonSituations":"Attempting Base64 via this op (Base64 is a separate operation); mistyping the base; defaulting an unset field to an out-of-range value.","solutions":["Set the Base argument to a value between 2 and 36 inclusive (16 for hex, 2 for binary, 8 for octal).","Use the dedicated 'To Base64' operation for Base64 encoding instead.","Confirm the argument metadata range matches the intended base."],"exampleFix":"// before: args[1] (Base) = 64  -> throws\n// after:  args[1] (Base) = 16  -> outputs hex char codes","handlingStrategy":"validation","validationCode":"const base = args[1];\nif (!(Number.isInteger(base) && base >= 2 && base <= 36)) {\n  throw new Error(\"Base must be an integer between 2 and 36\");\n}","typeGuard":"const isValidRadix = b => Number.isInteger(b) && b >= 2 && b <= 36;","tryCatchPattern":"try { toCharcode(input, [delim, base]); }\ncatch (e) { if (/Base argument/.test(e.message)) { base = 16; } else throw e; }","preventionTips":["Constrain the base input to 2-36 in the UI.","Use 16 for hex, 2 for binary, 8 for octal.","Use the dedicated Base64 operation for Base64."],"tags":["charcode","radix","validation","argument"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}