{"record":{"id":"e79595e918cdf402","repo":"TheAlgorithms/JavaScript","slug":"argument-is-not-a-valid-hex-code","errorCode":null,"errorMessage":"Argument is not a valid HEX code!","messagePattern":"Argument is not a valid HEX code!","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Conversions/HexToBinary.js","lineNumber":27,"sourceCode":"    6: '0110',\n    7: '0111',\n    8: '1000',\n    9: '1001',\n    a: '1010',\n    b: '1011',\n    c: '1100',\n    d: '1101',\n    e: '1110',\n    f: '1111'\n  })[key.toLowerCase()] // select the binary number by valid hex key with the help javascript object\n\nconst hexToBinary = (hexString) => {\n  if (typeof hexString !== 'string') {\n    throw new TypeError('Argument is not a string type')\n  }\n\n  if (/[^\\da-f]/gi.test(hexString)) {\n    throw new Error('Argument is not a valid HEX code!')\n  }\n  /*\n    Function for converting Hex to Binary\n\n    1. We convert every hexadecimal bit to 4 binary bits\n    2. Conversion goes by searching in the lookup table\n  */\n\n  return hexString.replace(/[0-9a-f]/gi, (lexeme) => binLookup(lexeme))\n}\n\nexport default hexToBinary\n","sourceCodeStart":9,"sourceCodeEnd":40,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/HexToBinary.js#L9-L40","documentation":"Thrown by hexToBinary when hexString contains any character that is not a hex digit. The regex /[^\\da-f]/gi allows only 0-9 and a-f/A-F; anything else (spaces, 0x prefix, 'g'-'z', punctuation) triggers it. Note this is a plain Error, not a TypeError, because the type is correct but the content is invalid.","triggerScenarios":"Calling hexToBinary('0xFF') (the 'x' is invalid), hexToBinary('ff ff') (space), hexToBinary('gg'), or hexToBinary('#ffffff') (the '#' fails). Even a trailing newline will trip it.","commonSituations":"Input carries a '0x' prefix from a formatter; pasted values include spaces or a leading '#'; CRLF newlines from a file line; mixed notation like 'hFF'.","solutions":["Strip prefixes/separators first: hexString.replace(/^0x/i, '').replace(/[^0-9a-f]/gi, '').","Trim whitespace and newlines before calling.","Validate with /^[0-9a-f]+$/i.test(input) and reject early with a clearer message."],"exampleFix":"// before\nhexToBinary('0xFF')\n// after\nhexToBinary('0xFF'.replace(/^0x/i, ''))","handlingStrategy":"validation","validationCode":"const clean = String(hexString).replace(/^0x/i, '').replace(/[^0-9a-f]/gi, '')\nif (!clean) throw new Error('No valid hex digits')\nreturn hexToBinary(clean)","typeGuard":"const isValidHex = (s) => typeof s === 'string' && /^[0-9a-f]+$/i.test(s)","tryCatchPattern":"try {\n  hexToBinary(hexString)\n} catch (e) {\n  if (/not a valid HEX code/.test(e.message)) {\n    return hexToBinary(hexString.replace(/^0x/i, '').trim())\n  }\n  throw e\n}","preventionTips":["Strip '0x' prefixes and '#' before calling.","Trim whitespace and newlines from pasted or file-read input.","Pre-validate with /^[0-9a-f]+$/i and reject early."],"tags":["validation","hex","regex","input-sanitization","conversions"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}