{"record":{"id":"f0d24b6da012b429","repo":"TheAlgorithms/JavaScript","slug":"invalid-hex-string","errorCode":null,"errorMessage":"Invalid hex string.","messagePattern":"Invalid hex string\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Conversions/HexToDecimal.js","lineNumber":3,"sourceCode":"function hexToInt(hexNum) {\n  if (!/^[0-9A-F]+$/.test(hexNum)) {\n    throw new Error('Invalid hex string.')\n  }\n  const numArr = hexNum.split('') // converts number to array\n  return numArr.map((item, index) => {\n    switch (item) {\n      case 'A':\n        return 10\n      case 'B':\n        return 11\n      case 'C':\n        return 12\n      case 'D':\n        return 13\n      case 'E':\n        return 14\n      case 'F':\n        return 15\n      default:\n        return parseInt(item)","sourceCodeStart":1,"sourceCodeEnd":21,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/HexToDecimal.js#L1-L21","documentation":"Thrown by hexToInt (used by hexToDecimal) when hexNum does not match /^[0-9A-F]+$/, i.e. it must be non-empty and contain ONLY uppercase A-F and digits. This is stricter than HexToBinary: lowercase hex, a '0x' prefix, empty string, or any non-hex char all fail. Plain Error (content validation, not type).","triggerScenarios":"Calling hexToDecimal('ff') (lowercase rejected), hexToDecimal('0xFF') (prefix + lowercase), hexToDecimal('') (empty fails the + quantifier), or hexToDecimal('GG').","commonSituations":"Lowercase hex from CSS colors (#ffffff) or JSON; values prefixed with '0x' from language literals; empty string from a missing field; lowercase output of another function fed in here.","solutions":["Uppercase and strip prefixes: const clean = hex.toUpperCase().replace(/^0x/, '').","Validate with /^[0-9A-F]+$/ before calling.","Handle empty input upstream instead of forwarding '' to the converter."],"exampleFix":"// before\nhexToDecimal('ff')\n// after\nhexToDecimal('ff'.toUpperCase())","handlingStrategy":"validation","validationCode":"const clean = String(hexNum).replace(/^0x/i, '').toUpperCase()\nif (!/^[0-9A-F]+$/.test(clean)) {\n  throw new Error('Invalid hex string')\n}\nreturn hexToDecimal(clean)","typeGuard":"const isValidUpperHex = (s) =>\n  typeof s === 'string' && /^[0-9A-F]+$/.test(s)","tryCatchPattern":"try {\n  hexToDecimal(hexNum)\n} catch (e) {\n  if (/Invalid hex string/.test(e.message)) {\n    return hexToDecimal(String(hexNum).replace(/^0x/i, '').toUpperCase())\n  }\n  throw e\n}","preventionTips":["Uppercase hex input before calling — the regex only allows A-F.","Strip '0x' prefixes at the boundary.","Reject empty strings upstream rather than forwarding them."],"tags":["validation","hex","regex","case-sensitivity","conversions"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}